文件复制程序
目的:
1.熟练掌握Swing界面的编写;
2.学会使用JFileChooser文件选择器的使用;
3.学会使用文件输入流FileInputStream和输出流FileOutputStream复制文件。
内容:
使用Swing基本组件和合适的布 局管理器编写文件复制程序界面,使用JFileChooser文件选择器,实现源文件和目标路径的图形化操作,文件复制程序具体操作流程如下:
(1)程序运行主界面
图1 程序主界面
(2)单击“浏览”按钮,弹出文件选择界面,选择需要复制的源文件
图2 选择文件界面
(3)同理操作,选择目标路径,然后单击“开始复制”完成文件复制
图3 文件复制成功
附加功能:在以上功能完成的基础上,可增加文件复制进度条功能。
代码如下:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Copy_Frame extends JFrame
//implements Runnable
{
//创建窗口组件
JProgressBar progressbar;
JLabel jl1 = new JLabel("源 文 件:");
JTextField jtf1 = new JTextField(25);
JButton jb1 = new JButton("浏览");
JLabel jl2 = new JLabel("目标路径:");
JTextField jtf2 = new JTextField(25);
JButton jb2 = new JButton("浏览");
JButton copyjb = new JButton("开始复制");
JLabel copylbed=new JLabel("复制已完成");
//构造函数
public Copy_Frame() {
try {
//界面设置成Windows风格
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//创建进度条
progressbar = new JProgressBar();
// 显示当前进度值信息
progressbar.setStringPainted(true);
// 设置进度条边框不显示
progressbar.setBorderPainted(false);
// 设置进度条的前景色
progressbar.setForeground(new Color(0, 210, 40));
// 设置进度条的背景色
progressbar.setBackground(new Color(188, 190, 194));
//设置进度条位置大小
progressbar.setBounds(0,270,500,30);
//设置窗口属性
this.setTitle("文件复制程序");
this.setLayout(new GridLayout(5, 1));
this.setSize(500, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//被复制文件控件
JPanel jp1 = new JPanel();
jp1.add(jl1);
jp1.add(jtf1);
jp1.add(jb1);
this.add(jp1);
jb1.addMouseListener(new ButtonListener());
//复制文件控件
JPanel jp2 = new JPanel();
jp2.add(jl2);
jp2.add(jtf2);
jp2.add(jb2);
this.add(jp2);
jb2.addMouseListener(new ButtonListener());
//开始复制控件
JPanel jp3 = new JPanel();
jp3.add(copyjb);
this.add(jp3);
copyjb.addMouseListener(new ButtonListener());//设置监听器
this.add(progressbar);
//进度条控件
JPanel jp4 = new JPanel();
jp4.add(progressbar);
this.add(jp4);
progressbar.setVisible(false);//隐藏进度条
//复制完成按钮控件
JPanel jp5= new JPanel();
jp5.add(copylbed);
this.add(jp5);
copylbed.setVisible(false);//隐藏文本
}
//使用内部类进行文件选择
class ButtonListener extends MouseAdapter{
@Override
public void mouseClicked(MouseEvent e) {
//点击开始复制后的操作
JFileChooser jc = new JFileChooser();
//若点击开始复制
if(e.getSource()==copyjb) {
progressbar.setVisible(true);//显示进度条
//开始新线程,开始复制,进度条开始加载
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
copying(jtf1.getText(),jtf2.getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
//点击需要复制的文件的"浏览"设置路径
if(e.getSource()==jb1) {
int val = jc.showOpenDialog(null);
if (val==JFileChooser.APPROVE_OPTION){//判断是否选中文件
String copyFromFilePath1 = jc.getSelectedFile().getAbsolutePath();//打印文件路径
System.out.println(copyFromFilePath1);//输出文件路径在控制台
jtf1.setText(copyFromFilePath1);//设置文本框的值为需要复制的文件路径
}
else{
System.out.println("没有选中文件!");//未选择文件
}
}
//点击需要被复制的文件的"浏览"设置路径
if(e.getSource()==jb2) {
jc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//只选择当前文件其目录,不进入文件内部
int val=jc.showOpenDialog(null);
if(val==JFileChooser.APPROVE_OPTION) {//判断是否选中文件路径
String copyFromFilePath2=jc.getSelectedFile().getAbsolutePath();//打印文件路径目录
System.out.println(copyFromFilePath2);//输出文件路径在控制台
jtf2.setText(copyFromFilePath2);//设置文本框的值为需要被复制的文件路径
}
else{
System.out.println("没有选中文件!");//未选择文件
}
}
}
}
public void copying(String copy,String be_copyed) throws IOException {//复制方法
FileInputStream in = new FileInputStream(copy);//文件输入流
//创建一个文件对象
File f=new File(copy);
//在目的地后面加上被复制文件名
FileOutputStream out = new FileOutputStream(be_copyed+"\\"+f.getName());
//拼接,在目录后加上文件名,表示要将此文件复制到哪个位置
byte[] b = new byte[1024];//设置缓存数组,每次缓存1024个字节
int n;//当前一次循环缓存字节数
long k;//需要缓存的次数
k=f.length()/1024+1;
long j=k/100;//进度条每进行1%需要进行的缓存次数
long y=j;//设置初值
long i=0;
int p=0;
int x=0;
//run();
//n是文件的字节数
while ((n=in.read(b))!=-1){
out.write(b,0,n);
i++;//缓存次数
if(i==j)
{
j+=y;//进度条下一次加载需要缓存的次数
p++;//进度条的值
progressbar.setValue(p);//设置进度条当前的值
System.out.println("文件复制当前进度"+p+"%");
if(p==100)
{
copylbed.setVisible(true);//弹出复制已完成label
}
}
}
//关闭文件输入输出流
in.close();
out.close();
System.out.println("文件复制完成!");
}
public static void main(String[] args) {
Copy_Frame ad = new Copy_Frame();
ad.setVisible(true);
}
//JOptionPane.showMessageDialog(this, "加载完成");
//this.dispose();关闭窗口,释放资源
//this.pack();使控件适合窗口
}
运行效果: