package copyfile;
import java.awt.BorderLayout;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.Rectangle;
public class CopyFile extends JFrame {
JLabel lbl_resoure = new JLabel();
JTextField txt_resoure = new JTextField();
JButton btn_find = new JButton();
JButton btn_copy = new JButton();
private File fileOld = null;
private File fileNew = null;
Container container;
JLabel lbl_new = new JLabel();
JTextField txt_new = new JTextField();
JButton btn_mark = new JButton(); ;
public CopyFile() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
container = this.getContentPane();
getContentPane().setLayout(null);
lbl_resoure.setText("原文件");
lbl_resoure.setBounds(new Rectangle(16, 13, 54, 24));
txt_resoure.setBounds(new Rectangle(85, 10, 115, 28));
btn_find.setToolTipText("");
txt_resoure.setEditable(false);
btn_copy.setBounds(new Rectangle(101, 73, 117, 27));
btn_copy.setText("复制");
btn_find.setBounds(new Rectangle(224, 12, 29, 26));
btn_find.setText("..");
lbl_new.setToolTipText("");
lbl_new.setText("新文件");
lbl_new.setBounds(new Rectangle(16, 45, 54, 24));
txt_new.setEditable(false);
txt_new.setBounds(new Rectangle(83, 41, 115, 28));
btn_mark.setBounds(new Rectangle(225, 43, 29, 26));
btn_mark.setToolTipText("");
btn_mark.setText("..");
container.add(btn_copy);
container.add(txt_resoure);
container.add(lbl_new);
container.add(txt_new);
container.add(lbl_resoure);
container.add(btn_find);
container.add(btn_mark);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(300, 150);
this.setTitle("复制文件练习");
this.setLocation((screen.width - 200) / 2, (screen.height - 100) / 2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
btn_find.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fileOld = findFile();
if (fileOld == null) {
JOptionPane.showMessageDialog(container, "没有找到要复制的文件");
} else {
txt_resoure.setText(fileOld.getName());
}
}
});
btn_mark.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
copyFile();
}
});
btn_copy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
toCopy() ;
}
});
}
private void copyFile() {
JFileChooser jfilechooser = new JFileChooser(FileSystemView.
getFileSystemView());
/* jfilechooser.set
String[] str = {"*.mdb", "*.Txt", "*.doc"};
for (int i = 0; i < str.length; i++) {
myFileFilter mff = new myFileFilter(str[i]);
jfilechooser.addChoosableFileFilter(mff);
}*/
int open_bool = jfilechooser.showSaveDialog(this);
if (jfilechooser.ERROR_OPTION != open_bool) {
{
fileNew = jfilechooser.getSelectedFile();
txt_new.setText(fileNew.getName());
}
}
}
private void toCopy() {
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(fileOld);
bis = new BufferedInputStream(fis);
fos = new FileOutputStream(fileNew);
bos = new BufferedOutputStream(fos);
int count = bis.available();
byte[] b = new byte[count];
bis.read(b, 0, count);
bos.write(b);
bos.flush();
//while()
} catch (Exception eIO) {
System.out.println(eIO.getMessage());
eIO.printStackTrace();
} finally {
try{
bis.close();
fis.close();
bos.close();
fos.close();
}catch(Exception eeee)
{}
}
}
private File findFile() {
JFileChooser jfilechooser = new JFileChooser(FileSystemView.
getFileSystemView());
int boolOpen = jfilechooser.showOpenDialog(this);
File file = null;
if (jfilechooser.ERROR_OPTION != boolOpen) {
file = jfilechooser.getSelectedFile();
}
return file;
}
public static void main(String[] args) {
new CopyFile();
}
}