Java 实现 文件的复制(GUI)

package text;

import java.awt.*;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;
public class ReadFile extends JDialog implements ActionListener {
 File file1, file2;

 JButton jb, opb1, opb2;

 TextField f1, f2;

 public ReadFile() {
  f1 = new TextField();
  f2 = new TextField();
  jb = new JButton("COPY");
  jb.addActionListener(this);
  opb1 = new JButton("...");
  opb1.addActionListener(this);
  opb2 = new JButton("...");
  opb2.addActionListener(this);
  JPanel panel = new JPanel();
  panel.setLayout(new GridLayout(1, 3));
  panel.add(new Panel());
  panel.add(jb);
  panel.add(new Panel());

  JPanel pane = new JPanel();
  BorderLayout bdl = new BorderLayout();
  pane.setLayout(bdl);
  pane.add(new JLabel("Copy    File   "), bdl.WEST);
  pane.add(f1, bdl.CENTER);
  pane.add(opb1, bdl.EAST);

  JPanel pan = new JPanel();
  BorderLayout bd = new BorderLayout();
  pan.setLayout(bd);
  pan.add(new JLabel("Copy File to "), bd.WEST);
  pan.add(f2, bd.CENTER);
  pan.add(opb2, bd.EAST);

  this.setLayout(new GridLayout(3, 1));
  this.add(pane);
  this.add(pan);
  this.add(panel);

  this.setSize(250, 100);
  this.setTitle("  Copy File  ");

 }

 boolean copystr(String s1, String s2) {

  try {
   file1 = new File(s1);
   file2 = new File(s2);
   FileInputStream in = new FileInputStream(file1);
   FileOutputStream out = new FileOutputStream(file2);
   byte buffer[] = new byte[1024];
   int c;
   while ((c = in.read(buffer)) != -1) {

    out.write(buffer, 0, c);
   }
   in.close();
   out.close();

  } catch (FileNotFoundException e) {
   JOptionPane.showMessageDialog(this, "Not found input File/n"
     + f1.getText() + "/n or /n" + f2.getText(), "ERROR",
     JOptionPane.WARNING_MESSAGE);
   e.printStackTrace();
   return false;
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }
  return true;

 }

 public void actionPerformed(ActionEvent e) {
  if (e.getSource() == jb) {
   String ff1 = f1.getText();
   String ff2 = f2.getText();
   boolean su = copystr(ff1, ff2);
   if (su)
    JOptionPane.showMessageDialog(this, "Copy File Seccussfull!/n"
      + f1.getText(), "SECCUEED", JOptionPane.NO_OPTION);
   else
    JOptionPane.showMessageDialog(this,
      "Copy File Unseccussfull!/n" + f1.getText(),
      "UNSECCUEED", JOptionPane.ERROR_MESSAGE);
  }
  if (e.getSource() == opb1) {
   FileDialog filed = new FileDialog(this, "Open File",
     FileDialog.LOAD);
   filed.setVisible(true);
   String name = filed.getFile();
   if (name != null) {
    f1.setText(filed.getDirectory() + name);

   } else {
    JOptionPane.showMessageDialog(this, "Open File Unseccussfull!",
      "UNSECCUEED", JOptionPane.ERROR_MESSAGE);
   }

  }
  if (e.getSource() == opb2) {
   FileDialog filed = new FileDialog(this, "Open File",
     FileDialog.LOAD);
   filed.setVisible(true);
   String name = filed.getFile();
   if (name != null) {
    f2.setText(filed.getDirectory() + name);

   } else {
    JOptionPane.showMessageDialog(this, "Open File Unseccussfull!",
      "UNSECCUEED", JOptionPane.ERROR_MESSAGE);
   }

  }

 }

 public static void main(String[] args) {
  ReadFile rf = new ReadFile();
  rf.setVisible(true);
 }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值