?? exam12_4.java
字號:
/* 程序名Exam12_4.java
* 文件復制應用程序
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exam12_4 extends JFrame implements ActionListener
{
JTextField source=new JTextField();
JTextField copy=new JTextField();
JButton exit=new JButton("退出");
JButton run=new JButton("復制");
public Exam12_4()
{
Container pane=this.getContentPane();
pane.setLayout(new GridLayout(0,2));
pane.add(new JLabel("源文件名"));
pane.add(source);
pane.add(new JLabel("備份文件名"));
pane.add(copy);
pane.add(exit);
pane.add(run);
exit.addActionListener(this);
run.addActionListener(this);
this.setTitle("文件復制示例");
this.setSize(300,200);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj==exit) System.exit(0);
if(obj!=run) return;
byte byteArray[]=new byte[128]; //定義字節數組
String ss="文件復制完成!!!";
try //捕獲I/O錯誤
{
FileInputStream file1=new FileInputStream(source.getText());//創建對象
FileOutputStream file2=new FileOutputStream(copy.getText());
int n=file1.read(byteArray); //從文件中輸入字節數據放入數組中
while(n!=-1)
{
file2.write(byteArray,0,n); //將字節數組寫入流中
n=file1.read(byteArray); //從文件中輸入字節數據放入數組中
}
}
catch(IOException e)
{
ss=e.toString(); //輸出出錯信息
}
JOptionPane.showMessageDialog(null,ss,"操作信息",1);
}
public static void main(String [] args)
{
new Exam12_4();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -