package copy;
import java.awt.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
public class CopyFile extends JFrame implements ActionListener{
JTextField srctext=new JTextField(20);
JButton srcbutton=new JButton("...");
JTextField totext=new JTextField(20);
JButton tobutton=new JButton("...");
JButton copybutton=new JButton("复制");
public CopyFile()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel srcpanel=new JPanel();
srcpanel.add(new JLabel("请选择要复制的文件:"));
srcpanel.add(srctext);
srcpanel.add(srcbutton);
srcbutton.addActionListener(this);
JPanel topanel=new JPanel();
topanel.add(new JLabel("请选择文件存放目录:"));
topanel.add(totext);
topanel.add(tobutton);
tobutton.addActionListener(this);
JPanel copypanel=new JPanel();
copypanel.add(copybutton);
copybutton.addActionListener(this);
this.setLayout(new GridLayout(3,1));
this.add(srcpanel);
this.add(topanel);
this.add(copypanel);
this.setLocation(300,300);
this.setResizable(false);
this.setTitle("文件复制器");
this.pack();
}
public void copyFile(String srcfile, String tofile) {
try {
int t;
FileReader fin = new FileReader(srcfile);
FileWriter fout = new FileWriter(tofile);
while ((t = fin.read()) > 0) {
System.out.print((char) t);
fout.write((char) t);
}
fin.close();
fout.flush();
fout.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==srcbutton)
{
JFileChooser chooser=new JFileChooser("");
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setDialogTitle("请选择要复制的文件");
if(chooser.showOpenDialog(this)==chooser.APPROVE_OPTION)
{
srctext.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
if(e.getSource()==tobutton)
{
JFileChooser chooser=new JFileChooser("");
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setDialogTitle("请选择保存到哪个文件");
if(chooser.showOpenDialog(this)==chooser.APPROVE_OPTION)
{
totext.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
if (e.getSource() == copybutton)
{
if (srctext.getText().equals("") || totext.getText().equals(""))
{
JOptionPane.showMessageDialog(this, "请设置源文件或目标路径");
}
else
{
File tofile = new File(totext.getText());
String filename = tofile.getName();
System.out.println(totext.getText());
System.out.println(tofile.getName());/////
this.copyFile(srctext.getText(), totext.getText() );
}
}
}
public static void main(String[] str)
{
JFrame.setDefaultLookAndFeelDecorated(true);
new CopyFile().show();
}
}
import java.awt.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
public class CopyFile extends JFrame implements ActionListener{
JTextField srctext=new JTextField(20);
JButton srcbutton=new JButton("...");
JTextField totext=new JTextField(20);
JButton tobutton=new JButton("...");
JButton copybutton=new JButton("复制");
public CopyFile()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel srcpanel=new JPanel();
srcpanel.add(new JLabel("请选择要复制的文件:"));
srcpanel.add(srctext);
srcpanel.add(srcbutton);
srcbutton.addActionListener(this);
JPanel topanel=new JPanel();
topanel.add(new JLabel("请选择文件存放目录:"));
topanel.add(totext);
topanel.add(tobutton);
tobutton.addActionListener(this);
JPanel copypanel=new JPanel();
copypanel.add(copybutton);
copybutton.addActionListener(this);
this.setLayout(new GridLayout(3,1));
this.add(srcpanel);
this.add(topanel);
this.add(copypanel);
this.setLocation(300,300);
this.setResizable(false);
this.setTitle("文件复制器");
this.pack();
}
public void copyFile(String srcfile, String tofile) {
try {
int t;
FileReader fin = new FileReader(srcfile);
FileWriter fout = new FileWriter(tofile);
while ((t = fin.read()) > 0) {
System.out.print((char) t);
fout.write((char) t);
}
fin.close();
fout.flush();
fout.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==srcbutton)
{
JFileChooser chooser=new JFileChooser("");
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setDialogTitle("请选择要复制的文件");
if(chooser.showOpenDialog(this)==chooser.APPROVE_OPTION)
{
srctext.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
if(e.getSource()==tobutton)
{
JFileChooser chooser=new JFileChooser("");
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setDialogTitle("请选择保存到哪个文件");
if(chooser.showOpenDialog(this)==chooser.APPROVE_OPTION)
{
totext.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
if (e.getSource() == copybutton)
{
if (srctext.getText().equals("") || totext.getText().equals(""))
{
JOptionPane.showMessageDialog(this, "请设置源文件或目标路径");
}
else
{
File tofile = new File(totext.getText());
String filename = tofile.getName();
System.out.println(totext.getText());
System.out.println(tofile.getName());/////
this.copyFile(srctext.getText(), totext.getText() );
}
}
}
public static void main(String[] str)
{
JFrame.setDefaultLookAndFeelDecorated(true);
new CopyFile().show();
}
}