public class HomeworkFive {
public static void main(String[] args) { new HomeworkFive().init();
JFrame jf=new JFrame("第五次作业"); JTextArea ita=new JTextArea(20,60); JButton jb1=new JButton("读取文件"); JButton ib2=new JButton("写入文件"); JFileChooser jfc=new JFileChooser("."); public void init(){
jb1.addActionListener(e ->{ jfc.showOpenDialog(jf);
File selectedFile =jfc.getSelectedFile(); try {
BufferedReader bis=new
BufferedReader(new FileReader(selectedFile));
char[] c=new char[1024]; int i=0;
while((i=bis.read(c))!=-1){
jta.append(new String(c,0,i));}
bis.close();
} catch (lOException o){
o.printStackTrace();
}
});
jb2.addActionListener(e->{ jfc.showSaveDialog(jf);
File selectedFile = jfc.getSelectedFile(); try {
BufferedWriter bis=new
BufferedWriter(new FileWriter(selectedFile));
bis.write(jta.getText()); bis.close();
} catch (lOException o) {
o.printStackTrace();
});
Box box1= Box.createHorizontalBox(); Box box2 = Box.createVerticalBox(); box1.add(jb1); box1.add(jb2);
box2.add(new JScrollPane(jta)) box2.add(box1); jfadd(box2);
jf.setVisible(true); jf.pack();
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}