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);
}
}
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);
}
}
相关文章推荐
- java实现gui界面的文件复制
- Java实现对于文件的多种操作,复制、删除、重命名、剪切文件、统计指定类型文件数等10个功能
- 复制一个文件夹下所有文件(包括子文件夹)并修改文件后缀名 java实现加密文档unlock
- java实现文件的复制,移动
- Java实现文件批量复制避免文件 4000 名重复并且重命名
- java NIO实现的文件复制(包括文件与文件夹复制) NIO读取数据与写数据
- java 实现文件复制,二种方法.
- 一道Java面试题之实现复制、删除、剪切文件(文件夹的实现)的snippet
- 实现文件复制、剪切、删除操作 Java
- java 实现文件复制的两种方式
- java中的文件输入输出流,实现复制粘贴
- java字节流实现文件复制
- java字节流实现文件复制
- java字节流实现文件复制
- java实现两台服务器间文件复制的方法
- java 实现文件复制和格式更改的实例
- java实现基于关键字的文件夹(文件)的搜索、文件夹(文件)的复制、删除
- Java中实现复制文件到文件,复制文件到文件夹,复制文件夹到文件,删除文件,删除文件夹,移动文件,移动文件夹的工具类
- java io实现文件复制
- java实现文件复制