import java.io.FileInputStream;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.ProgressMonitorInputStream;
public class ProgressMonitorTest {
public void useProgressMonitor(JFrame frame, String copyPath, String newPath) {
try {
File file = new File(copyPath);
File newFile = new File(newPath);
FileOutputStream fop = new FileOutputStream(newFile);
InputStream in = new FileInputStream(file);
ProgressMonitorInputStream pm = new ProgressMonitorInputStream(
frame, "文件读取中,请稍后...", in);
int c = 0;
byte[] bytes = new byte[102];
while ((c = pm.read(bytes)) != -1) {
fop.write(bytes, 0, c);
}
fop.close();
pm.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Swing extends JFrame implements Runnable{ //继承JFrame使自己成为一个窗口
private static final long serialVersionUID = 8674569541853793419L;
private JPanel contentPane;
private JTextField fileField;
private JTextField searchTextField;
private JTextField replaceTextField;
private File file;
private JTextField textField;
private JTextField textField_1;
private int sign=1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Swing frame = new Swing();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Swing() {
setResizable(false); //设置此窗体是否可由用户调整大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //可以起到关闭程序与停止dos运行的目的.
setBounds(100, 100, 501, 184);
setTitle("在读取文件时使用进度条");
getContentPane().setLayout(null);
JLabel label = new JLabel("文件地址:");
label.setBounds(10, 10, 70, 15);
getContentPane().add(label); //初始化一个容器,用来在容器上添加一些控件
textField = new JTextField(); //文本框
textField.setBounds(90, 7, 300, 21);
getContentPane().add(textField);
textField.setColumns(10);
JButton button = new JButton("选择文件");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
}
});
button.setBounds(400, 6, 93, 23);
getContentPane().add(button);
JLabel label_1 = new JLabel("复制地址:");
label_1.setBounds(10, 40, 70, 15);
getContentPane().add(label_1);
textField_1 = new JTextField();
textField_1.setBounds(90, 38, 300, 21);
getContentPane().add(textField_1);
textField_1.setColumns(10);
JButton button_1 = new JButton("选择地址");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_1_actionPerformed(e);
}
});
button_1.setBounds(400, 39, 93, 23);
getContentPane().add(button_1);
JButton button_2 = new JButton("开始复制");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_copyButton_actionPerformed(e);
}
});
button_2.setBounds(100, 69, 93, 23);
getContentPane().add(button_2);
JButton button_3 = new JButton("开始移动");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sign=0;
do_moveButton_actionPerformed(e);
}
});
button_3.setBounds(250, 69, 93, 23);
getContentPane().add(button_3);
}
protected void do_button_actionPerformed(ActionEvent e){
JFileChooser chooser=new JFileChooser(); //打开文件选择器
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); //只能选择文件
// 显示文件打开对话框
int option = chooser.showOpenDialog(this); //这个参数决定你弹出文件选择器的位置,null———当前电脑显示器屏幕的中央,this———当前你编写的程序屏幕中央
// 确定用户按下打开按钮,而非取消按钮
if (option != JFileChooser.APPROVE_OPTION)
return;
// 获取用户选择的文件对象
file = chooser.getSelectedFile();
// 显示文件信息到文本框
textField.setText(file.toString());
}
protected void do_button_1_actionPerformed(ActionEvent e){
JFileChooser chooser=new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); //只能选择目录
int option=chooser.showOpenDialog(this);
if(option!=JFileChooser.APPROVE_OPTION)
return;
file=chooser.getSelectedFile();
textField_1.setText(file.toString());
}
//确定复制按钮单击事件
protected void do_copyButton_actionPerformed(ActionEvent arg0) {
Thread thread = new Thread(this);
thread.start();
}
protected void do_moveButton_actionPerformed(ActionEvent arg0) {
Thread thread = new Thread(this);
thread.start();
}
//应用多线程技术实现读取操作
@Override
public void run() {
ProgressMonitorTest test = new ProgressMonitorTest();
String path = textField.getText();
String save = textField_1.getText();
test.useProgressMonitor(this,path,save+path.substring(path.lastIndexOf("\\"),path.length()));
if(sign==0){
File f = new File(path);
f.delete();
sign=1;
}
}
}