您的位置:首页 > 产品设计 > UI/UE

Java 实现 文件的复制(GUI)

2007-11-03 10:08 489 查看
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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: