在java中,进度条不会显示在从一个驱动器复制到另一个程序的文件中
我有一个程序,它用java编写,将文件从一个驱动器复制到另一个驱动器。一切正常。我添加了一个进度条来指示文件复制的进度。它没有出现。下面是复制文件的代码。不知道我哪里出了问题。文件大小显示正确,但进度条不显示在java中,进度条不会显示在从一个驱动器复制到另一个程序的文件中,java,progress-bar,file-copying,Java,Progress Bar,File Copying,我有一个程序,它用java编写,将文件从一个驱动器复制到另一个驱动器。一切正常。我添加了一个进度条来指示文件复制的进度。它没有出现。下面是复制文件的代码。不知道我哪里出了问题。文件大小显示正确,但进度条不显示 import java.awt.*; import java.awt.event.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; impor
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.lang.*;
public class CopyGUI1 extends JFrame implements ActionListener, ChangeListener
{
private JLabel fromLabel, zoneLabel;
private JTextField fromField, zoneField;
private JButton beginButton, amd, newSO;
private String to = "C:\\DCIN_TER\\DCIN_EPU2\\CIRCUIT FROM BRANCH\\Rainbow orders\\";
private String[] zone = {"SZ","NZ","WZ","EZ","JW","AZ"};
private JComboBox c;
private static JProgressBar progressBar;
int count = 0;
private long fileLength;
//Main method
public static void main(String[] args)
{
new CopyGUI1().setVisible(true);
}
public CopyGUI1()
{
init();
}
private void init()
{
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setBounds(300, 300, 500, 400);
setResizable(false);
setTitle("Copy SO from C:\\01_ORDER_PROCESSING\\ drive to C:\\DCIN_TER\\DCIN_EPU2\\ drive");
JPanel panel = new JPanel(null);
add(panel);
//add button and lebel
fromLabel = new JLabel("SO No.:");
fromLabel.setBounds(40, 20, 50, 20);
fromField = new JTextField();
fromField.setBounds(100, 20, 200, 20);
panel.add(fromLabel);
panel.add(fromField);
zoneLabel = new JLabel("Zone:");
zoneLabel.setBounds(40, 40, 50, 20);
zoneField = new JTextField();
zoneField.setBounds(140, 40, 200, 20);
panel.add(zoneLabel);
panel.add(zoneField);
c = new JComboBox();
for(int i = 0; i < 6; i++)
c.addItem(zone[count++]);
c.setEditable(false);
c.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
zoneField.setText("" + c.getSelectedItem());
}
});
c.setBounds(100, 40, 40, 20);
panel.add(c);
//add progress bar
progressBar = new JProgressBar();
progressBar.setBounds(50, 100, 300, 30);
progressBar.addChangeListener(this);
panel.add(progressBar);
beginButton = new JButton("Begin Copy");
beginButton.setBounds(140, 200, 150, 20);
beginButton.addActionListener(this);
panel.add(beginButton);
amd = new JButton("Copy Amendments");
amd.setBounds(140, 220, 150, 20);
amd.addActionListener(this);
panel.add(amd);
newSO = new JButton("Add a new SO");
newSO.setBounds(140, 240, 150, 20);
newSO.addActionListener(this);
panel.add(newSO);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource () == beginButton)
copy();
else if (e.getSource() == amd)
{
CopyAmd c = new CopyAmd();
c.show();
}
else if (e.getSource() == newSO)
{
CopyGUI1 cg = new CopyGUI1();
cg.show();
}
}
public void copy()
{
if(("").equals(fromField.getText()))
{
JOptionPane.showMessageDialog(this, "Input the SO Number!");
return;
}
if(("").equals(zoneField.getText()))
{
JOptionPane.showMessageDialog(this, "Input the zone!");
return;
}
Runnable r1 = new Runnable()
{
public void run()
{
final File srcFolder = new File("C:\\01_ORDER_PROCESSING\\IN11\\" + zoneField.getText() + "\\" + fromField.getText() + "\\");
File destFolder = new File(to + fromField.getText() + "\\");
if(destFolder.exists())
{
int op = JOptionPane.showConfirmDialog(null, destFolder.getName()+ " file exists! \n Do you want to overwrite it?","Confirm Window", JOptionPane.YES_NO_OPTION);
if (op == JOptionPane.NO_OPTION)
{
JOptionPane.showMessageDialog(null, "Copy Canceled");
fromField.setText("");
zoneField.setText("");
return;
}
else
{
//make sure source exists
if(!(srcFolder.exists()))
{
JOptionPane.showMessageDialog(null, "Directory does not exist. on " + srcFolder.getAbsolutePath() + " and hence, no files to copy.");
//just exit
dispose();
}
else
{
try
{
fileLength = srcFolder.length();
System.out.println("fileLength : " + fileLength);
progressBar.setMaximum((int) fileLength);
final long startTimeMillis = System.currentTimeMillis();
copyFolder(srcFolder, destFolder);
File file = new File(destFolder + "\\PADR Release\\");
if(!(file.exists()))
file.mkdir();
final long elapsedTimeMillis = System.currentTimeMillis() - startTimeMillis;
JOptionPane.showMessageDialog(null, "Copy Task Completed Successfully. The Task took " + elapsedTimeMillis + " milli seconds to complete!!");
dispose();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
else
{
//make sure source exists
if(!(srcFolder.exists()))
{
JOptionPane.showMessageDialog(null, "Directory does not exist. on " + srcFolder.getAbsolutePath() + " and hence, no files to copy.");
//just exit
dispose();
}
else
{
try
{
fileLength = srcFolder.length();
System.out.println("fileLength : " + fileLength);
progressBar.setMaximum((int) fileLength);
final long startTimeMillis = System.currentTimeMillis();
copyFolder(srcFolder, destFolder);
File file = new File(destFolder + "\\PADR Release\\");
if(!(file.exists()))
file.mkdir();
final long elapsedTimeMillis = System.currentTimeMillis() - startTimeMillis;
JOptionPane.showMessageDialog(null, "Copy Task Completed Successfully. The Task took " + elapsedTimeMillis + " milli seconds to complete!!");
dispose();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
};
Thread t1 = new Thread(r1);
t1.start();
}
public void stateChanged(ChangeEvent e)
{
if (e.getSource() == progressBar)
{
if (progressBar.getValue() == progressBar.getMaximum())
{
//JOptionPane.showMessageDialog(this, "Copy Over");
progressBar.setValue(0);
}
}
}
public static void copyFolder(File src, File dest) throws IOException
{
if(src.isDirectory())
{
//if directory does not exist, create it
if(!dest.exists())
{
dest.mkdir();
System.out.println("Directory copied from " + src + " to " + dest);
}
//list all the directory contents
String files[] = src.list();
for (String file : files)
{
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
//recursive copy
copyFolder(srcFile, destFile);
}
}
else
{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
int flag = 0;
while ((length = in.read(buffer)) > 0)
{
out.write(buffer, 0, length);
flag += length;
progressBar.setValue(flag);
}
in.close();
out.close();
System.out.println("File copied from " + src + " to " + dest);
}
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.*;
导入javax.swing.JComboBox;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.event.ChangeEvent;
导入javax.swing.event.ChangeListener;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.*;
导入java.lang.*;
公共类CopyGUI1扩展JFrame实现ActionListener、ChangeListener
{
私人JLabel fromLabel,zoneLabel;
私有JTextField fromField,zoneField;
私人JButton beginButton,amd,newSO;
私有字符串to=“C:\\DCIN\u TER\\DCIN\u EPU2\\CIRCUIT FROM BRANCH\\Rainbow orders\\”;
私有字符串[]区域={“SZ”、“NZ”、“WZ”、“EZ”、“JW”、“AZ”};
私人JComboBox c;
私有静态JProgressBar progressBar;
整数计数=0;
私有长文件长度;
//主要方法
公共静态void main(字符串[]args)
{
新建CopyGUI1().setVisible(true);
}
公共CopyGUI1()
{
init();
}
私有void init()
{
setDefaultCloseOperation(在关闭时处理);
立根(300300500400);
可设置大小(假);
setTitle(“将SO从C:\\01\u订单处理\\驱动器复制到C:\\DCIN\u TER\\DCIN\u EPU2\\drive”);
JPanel面板=新JPanel(空);
添加(面板);
//添加按钮和lebel
fromLabel=新的JLabel(“SO编号:”);
fromLabel.setBounds(40,20,50,20);
fromField=newjtextfield();
fromField.setBounds(100,20,200,20);
panel.add(fromlab);
panel.add(fromField);
zoneLabel=新JLabel(“区域:”);
后退带(40,40,50,20);
zoneField=新的JTextField();
分区.立根(140,40,200,20);
面板。添加(zoneLabel);
面板。添加(分区域);
c=新的JComboBox();
对于(int i=0;i<6;i++)
c、 addItem(区域[count++]);
c、 可编辑设置(假);
c、 addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
zoneField.setText(“+c.getSelectedItem());
}
});
c、 立根(100,40,40,20);
小组委员会.添加(c);
//添加进度条
progressBar=新的JProgressBar();
进度条立根(50、100、300、30);
progressBar.addChangeListener(此);
面板。添加(进度条);
beginButton=新JButton(“开始复制”);
开始钮扣。挫折(140,200,150,20);
addActionListener(this);
面板。添加(开始按钮);
amd=新的JButton(“复制修正案”);
amd.setBounds(14022015020);
amd.addActionListener(本);
补充(amd);
newSO=新的JButton(“添加新的SO”);
新闻界(140、240、150、20);
newSO.addActionListener(本);
面板。添加(新闻);
}
已执行的公共无效操作(操作事件e)
{
if(e.getSource()==beginButton)
复制();
else if(例如getSource()==amd)
{
CopyAmd c=新的CopyAmd();
c、 show();
}
else if(e.getSource()==newSO)
{
CopyGUI1 cg=新的CopyGUI1();
cg.show();
}
}
公开作废副本()
{
if((“”)等于(fromField.getText())
{
showMessageDialog(这是“输入SO编号!”);
回来
}
如果((“”)等于(zoneField.getText())
{
showMessageDialog(这是“输入区域!”);
回来
}
Runnable r1=新的Runnable()
{
公开募捐
{
final File srcFolder=新文件(“C:\\01\u订单处理\\IN11\\”+zoneField.getText()+“\\”+fromField.getText()+“\\”;
File destFolder=新文件(到+fromField.getText()+“\\”;
如果(destFolder.exists())
{
int op=JOptionPane.showConfirmDialog(null,destFolder.getName()+“文件存在!\n是否覆盖它?”,“确认窗口”,JOptionPane.YES\u NO\u选项);
if(op==JOptionPane.NO_选项)
{
showMessageDialog(空,“复制Ca”