----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------
刚写了个copy文件的小程序,虽然简陋了点,但还是蛮有成就感的,顺便说一下,我用记事本写的hiahia!
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class copy{
public static void main(String[] args){
new Thread(new Window()).start();
}
}
class CopyBridge{
private static CopyBridge cb = new CopyBridge();
private String source = null;
private String target = null;
private boolean flag = false;
private String message = null;
private boolean mflag = false;
private String messageDialog = null;
private CopyBridge(){}
public static CopyBridge getCopyBridge(){
return cb;
}
public void setsource(String source){
this.source = source;
}
public String getsource(){
return source;
}
public void settarget(String target){
this.target = target;
}
public String gettarget(){
return target;
}
public void setflag(boolean flag){
this.flag = flag;
}
public boolean getflag(){
return flag;
}
public void setMessageDialog(String string){
this.messageDialog = string;
mflag = true;
}
public String getMessageDialog(){
mflag = true;
return messageDialog;
}
public boolean getmflag(){
return mflag;
}
}
class CopyData implements Runnable{
public void run(){
byte[] bytes = new byte[1024*1024];
FileInputStream filein = null;
FileOutputStream fileout = null;
CopyBridge cb = CopyBridge.getCopyBridge();
if(cb.getflag()){
try{
filein = new FileInputStream(cb.getsource());
}
catch(FileNotFoundException e){
cb.setMessageDialog("您输入的source地址不对");
}
try{
fileout = new FileOutputStream(cb.gettarget());
}
catch(FileNotFoundException e){
cb.setMessageDialog("您输入的target地址不对,请重新输入");
}
try{
filein.read(bytes);
fileout.write(bytes);
}
catch(IOException e){
cb.setMessageDialog("写入时错误");
}
cb.setMessageDialog("copy完毕");
cb.setflag(false);
}
}
}
class Window implements Runnable{
public void run(){
/*
* 以下语句用于创建一个window和一个dialog并对其进行初始化
* 该窗口由3个label、2个textfield、1个button组成,dialog由一个label、一个button组成
*/
Frame frame = new Frame("copy");
final Dialog dialog = new Dialog(frame,"提示信息",true);//定义为final以便定义在局部内部类能够访问
Label label = new Label("————————该小程序用于复制文件————————",Label.CENTER);
Label sourcelabel = new Label("source:",Label.CENTER);
Label targetlabel = new Label(" target:",Label.CENTER);
label.setSize(new Dimension(20,80));
sourcelabel.setSize(new Dimension(20,60));
targetlabel.setSize(new Dimension(20,60));
final TextField sourcetextfield = new TextField("请在这里输入source",40);
final TextField targettextfield = new TextField("请在这里输入target",40);
Button button = new Button(" 确定 ");
Button buttondialog = new Button("确定");
final Label labeldialog = new Label("结果将显示在这里",Label.CENTER);
labeldialog.setSize(new Dimension(50,350));
frame.setBounds(300,200,400,200);
frame.setResizable(false);
frame.setLayout(new FlowLayout());
frame.add(label);
frame.add(sourcelabel);
frame.add(sourcetextfield);
frame.add(targetlabel);
frame.add(targettextfield);
frame.add(button);
dialog.setBounds(350,250,350,120);
dialog.setResizable(false);
dialog.setLayout(new FlowLayout());
dialog.add(labeldialog);
dialog.add(buttondialog);
frame.setVisible(true);
/*
*
* 以下语句用于定义事件
*/
//定义窗口时的监听事件,用于点击关闭按钮时退出程序
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//定义对话框的监听事件,用于关闭对话框
dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dialog.setVisible(false);
}
});
//定义对话框中按钮监控事件,用于关闭对话框
buttondialog.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dialog.setVisible(false);
}
});
//定义窗口中按钮的监控事件
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
CopyBridge cb = CopyBridge.getCopyBridge();
new Thread(new CopyData()).start();
cb.setsource(sourcetextfield.getText());
cb.settarget(targettextfield.getText());
cb.setflag(true);
while(true){
if(cb.getmflag()){
labeldialog.setText(cb.getMessageDialog());
dialog.setVisible(true);
break;
}
}
}
});
}
}
今天才发现,这个小程序bug还是蛮多的,最大只能拷贝1M文件,而且输入文件的格式必须满足要求……
不过实在不想再改了,反正也只是练手目的达到就可以了。