复制文件

本文介绍了一个使用Java Swing创建的简单图形用户界面应用程序,该程序可以实现文件的复制功能。用户可以通过界面上的按钮选择源文件及目标路径,并执行复制操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值