当堂程序:一个有两个输入框,一个按钮的图形界面窗口,程序拥有复制文件的功能,在文件名框内输入待复制文件的地址(/改为//),复制到框内就不用我说了。。。按下确定按钮完成复制
代码如下
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class text3 {
static Frame frame = null;
static Label l1 = null;
static Label l2 = null;
static TextArea t1 = null;
static TextArea t2 = null;
static Button button = null;
static File f1 = null;
static File f2 = null;
static FileReader fReader = null;
static FileWriter fWriter = null;
static int a;
public static void main(String[] args){
frame = new Frame();
frame.setVisible(true);
frame.setLayout(null);
frame.setBounds(200, 100, 320, 400);
l1 = new Label("文件名:");
l2 = new Label("复制到:");
t1 = new TextArea();
t2 = new TextArea();
button = new Button("确定");
frame.add(l1);
frame.add(l2);
frame.add(t1);
frame.add(t2);
l1.setBounds(20, 40, 70, 40);
l2.setBounds(20, 100, 70, 40);
t1.setBounds(90, 40, 200, 40);
t2.setBounds(100, 150, 100, 40);
button.setBounds(100, 150, 100, 40);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f1 = new File(t1.getText());
f2 = new File(t2.getText());
try {
fReader = new FileReader(f1);
} catch (FileNotFoundException e1) {
}
try {
fWriter = new FileWriter(f2);
} catch (IOException e1) {
}
try {
a = fReader.read();
} catch (IOException e1) {
}
while (a!=-1) {
try {
fWriter.write((char)a);
} catch (IOException e1) {
}
try {
a = fReader.read();
} catch (IOException e1) {
}
}
try {
fReader.close();
} catch (IOException e1) {
}
try {
fWriter.close();
} catch (IOException e1) {
}
}
});
}
}