import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Filetest4 extends JFrame{
public static JTextArea getjt(){
JTextArea jt=new JTextArea();
return jt;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf=new JFrame();
Container c=jf.getContentPane();
JScrollPane js=new JScrollPane();
js.setViewportView(getjt());
JButton open=new JButton("写入文件");
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
File file=new File("word.txt");
try{
FileWriter out=new FileWriter(file);
String s=getjt().getText();
out.write(s);
out.close();
}catch(Exception o){
o.printStackTrace();
}
}
});
JButton close=new JButton("读取文件");
close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
File file=new File("word.txt");
try{
FileReader in=new FileReader(file);
char[] ch=new char[1024];
int len=in.read(ch);
getjt().setText(new String(ch,0,len));
in.close();
}catch(Exception o){
o.printStackTrace();
}
}
});
JPanel jp=new JPanel(new FlowLayout());
jp.add(open);
jp.add(close);
c.setLayout(new BorderLayout());
c.add(js,BorderLayout.CENTER);
c.add(jp,BorderLayout.SOUTH);
jf.setSize(200,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}