Java 复制文件时如何使用ProgressMonitorInputStream?
我试图用java将文件从一个地方复制到另一个地方。我正在使用Java 复制文件时如何使用ProgressMonitorInputStream?,java,swing,file-io,copy,progress-bar,Java,Swing,File Io,Copy,Progress Bar,我试图用java将文件从一个地方复制到另一个地方。我正在使用FileInputStream和 FileOutputStream复制。这是我的密码: private JPanel contentPane; private JButton btnCopy; private JFileChooser SelectFile; private JFileChooser ChooseDestination; private File Selectedfile; private File chosenDest
FileInputStream
和
FileOutputStream
复制。这是我的密码:
private JPanel contentPane;
private JButton btnCopy;
private JFileChooser SelectFile;
private JFileChooser ChooseDestination;
private File Selectedfile;
private File chosenDestination;
protected void Copy() {
// TODO Auto-generated method stub
int answer = SelectFile.showOpenDialog(this);
if(answer==0){
this.Selectedfile = SelectFile.getSelectedFile();
int answer2 = ChooseDestination.showSaveDialog(this);
if(answer2==0){
this.chosenDestination = ChooseDestination.getSelectedFile();
try {
FileInputStream Read = new FileInputStream(Selectedfile);
FileOutputStream write = new FileOutputStream(chosenDestination + Selectedfile.getName());
InputStream in = new BufferedInputStream(new ProgressMonitorInputStream(main.this,"Copying", new FileInputStream(Selectedfile)));
int Data;
byte[] buffer = new byte[1024];
while((Data= in.read(buffer))>0){
write.write(buffer);
}//Here the progressmMonitorInputStream window opens but there is nothing in the window.
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
它静止不动,直到文件拷贝完成。但文件复制正确。我只想在单独的窗口中显示进度条。请帮帮我。谢谢。请参阅。请参阅。以下代码会有所帮助:
import java.awt.BorderLayout;
public class Main extends JFrame implements PropertyChangeListener {
private JPanel contentPane;
private JButton btnCopy;
private JFileChooser selectFile;
private JFileChooser chooseDestination;
private File selectedfile;
private File chosenDestination;
private JProgressBar progressBar;
private Copy copy;
class Copy extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
int answer = selectFile.showOpenDialog(Main.this);
if (answer == 0) {
Main.this.selectedfile = selectFile.getSelectedFile();
int answer2 = chooseDestination.showSaveDialog(Main.this);
if (answer2 == 0) {
Main.this.chosenDestination = chooseDestination
.getSelectedFile();
try {
FileInputStream fileInputStream = new FileInputStream(
selectedfile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(
fileInputStream);
ProgressMonitorInputStream progressMonitorInputStream = new ProgressMonitorInputStream(
Main.this, "Copying", bufferedInputStream);
File outputFile = new File(chosenDestination
+ selectedfile.getName());
FileOutputStream fileOutputStream = new FileOutputStream(
outputFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
fileOutputStream);
int data;
byte[] buffer = new byte[1024];
while ((data = progressMonitorInputStream.read(buffer)) > 0) {
bufferedOutputStream.write(buffer); }
bufferedOutputStream.close();
progressMonitorInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/*
* Executed in event dispatching thread
*/
@Override
public void done() {
Toolkit.getDefaultToolkit().beep();
btnCopy.setEnabled(true);
setCursor(null); // turn off the wait cursor
}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*
* @throws UnsupportedLookAndFeelException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
public Main() throws ClassNotFoundException, InstantiationException,
IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 130, 135);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new MigLayout("", "[89px][][][][][][][]", "[23px][][]"));
btnCopy = new JButton("Copy");
btnCopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
copy = new Copy();
copy.addPropertyChangeListener(Main.this);
copy.execute();
}
});
panel.add(btnCopy, "cell 1 1,grow");
selectFile = new JFileChooser();
chooseDestination = new JFileChooser();
chooseDestination.setFileSelectionMode(2);
pack();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
progressBar.setValue(progress);
}
}
}
导入java.awt.BorderLayout;
公共类Main扩展JFrame实现PropertyChangeListener{
私有JPanel内容窗格;
私人JButton btnCopy;
私有JFileChooser selectFile;
private JFileChooser chooseDestination;
私有文件选择文件;
私有文件选择;
私人JProgressBar progressBar;
私人副本;
类副本扩展了SwingWorker{
@凌驾
受保护的Void doInBackground()引发异常{
int answer=selectFile.showOpenDialog(Main.this);
如果(答案=0){
Main.this.selectedfile=selectFile.getSelectedFile();
int answer2=选择destination.showsavedilog(Main.this);
如果(回答2==0){
Main.this.chosenDestination=选择Destination
.getSelectedFile();
试一试{
FileInputStream FileInputStream=新FileInputStream(
所选文件);
BufferedInputStream BufferedInputStream=新的BufferedInputStream(
文件输入流);
ProgressMonitorInputStream ProgressMonitorInputStream=新的ProgressMonitorInputStream(
Main.this,“复制”,bufferedInputStream);
File outputFile=新文件(chosenDestination
+selectedfile.getName());
FileOutputStream FileOutputStream=新的FileOutputStream(
输出文件);
BufferedOutputStream BufferedOutputStream=新的BufferedOutputStream(
文件输出流);
int数据;
字节[]缓冲区=新字节[1024];
而((数据=progressMonitorInputStream.read(缓冲区))>0){
bufferedOutputStream.write(缓冲区);}
bufferedOutputStream.close();
progressMonitorInputStream.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
返回null;
}
/*
*在事件调度线程中执行
*/
@凌驾
公众假期结束(){
getDefaultToolkit().beep();
btnCopy.setEnabled(true);
setCursor(null);//关闭等待光标
}
}
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
主机架=新主机架();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*
*@抛出不受支持的LookandFeelException
*@galacessException
*@throws实例化异常
*@ClassNotFoundException
*/
public Main()抛出ClassNotFoundException、InstanceionException、,
非法访问异常,不受支持的OOK和FEELEXCEPTION{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100130135);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
setLayout(新的BorderLayout(0,0));
setContentPane(contentPane);
progressBar=新的JProgressBar(01100);
progressBar.setValue(0);
progressBar.SetStringPaint(真);
JPanel面板=新的JPanel();
contentPane.add(面板,BorderLayout.CENTER);
panel.setLayout(新的MigLayout(“,”[89px][]”,“[23px][]”));
btnCopy=新的JButton(“复制”);
btnCopy.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
复制=新复制();
copy.addPropertyChangeListener(Main.this);
copy.execute();
}
});
添加(btnCopy,“单元格1,生长”);
selectFile=newjfilechooser();
chooseDestination=新建JFileChooser();
选择Destination.setFileSelectionMode(2);
包装();
}
@凌驾
公共作废属性更改(属性更改事件evt){
if(“progress”==evt.getPropertyName()){
int progress=(整数)evt.getNewValue();
progressBar.setValue(进度);
}
}
}
以下代码会有所帮助:
import java.awt.BorderLayout;
public class Main extends JFrame implements PropertyChangeListener {
private JPanel contentPane;
private JButton btnCopy;
private JFileChooser selectFile;
private JFileChooser chooseDestination;
private File selectedfile;
private File chosenDestination;
private JProgressBar progressBar;
private Copy copy;
class Copy extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
int answer = selectFile.showOpenDialog(Main.this);
if (answer == 0) {
Main.this.selectedfile = selectFile.getSelectedFile();
int answer2 = chooseDestination.showSaveDialog(Main.this);
if (answer2 == 0) {
Main.this.chosenDestination = chooseDestination
.getSelectedFile();
try {
FileInputStream fileInputStream = new FileInputStream(
selectedfile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(
fileInputStream);
ProgressMonitorInputStream progressMonitorInputStream = new ProgressMonitorInputStream(
Main.this, "Copying", bufferedInputStream);
File outputFile = new File(chosenDestination
+ selectedfile.getName());
FileOutputStream fileOutputStream = new FileOutputStream(
outputFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
fileOutputStream);
int data;
byte[] buffer = new byte[1024];
while ((data = progressMonitorInputStream.read(buffer)) > 0) {
bufferedOutputStream.write(buffer); }
bufferedOutputStream.close();
progressMonitorInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/*
* Executed in event dispatching thread
*/
@Override
public void done() {
Toolkit.getDefaultToolkit().beep();
btnCopy.setEnabled(true);
setCursor(null); // turn off the wait cursor
}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*
* @throws UnsupportedLookAndFeelException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
public Main() throws ClassNotFoundException, InstantiationException,
IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 130, 135);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new MigLayout("", "[89px][][][][][][][]", "[23px][][]"));
btnCopy = new JButton("Copy");
btnCopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
copy = new Copy();
copy.addPropertyChangeListener(Main.this);
copy.execute();
}
});
panel.add(btnCopy, "cell 1 1,grow");
selectFile = new JFileChooser();
chooseDestination = new JFileChooser();
chooseDestination.setFileSelectionMode(2);
pack();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
progressBar.setValue(progress);
}
}
}
导入java.awt.BorderLayout;
公共类Main扩展JFrame实现PropertyChangeListener{
私有JPanel内容窗格;
私人JButton btnCopy;
私有JFileChooser selectFile;
private JFileChooser chooseDestination;
私有文件选择文件;
私有文件选择;
私人JProgressBar progressBar;
私人副本;
类副本扩展了SwingWorker{
@凌驾
受保护的Void doInBackground()引发异常{
int answer=selectFile.showOpenDialog(Main.this);
如果(答案=0){