package com.cissst;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.PopupMenu;
import java.awt.ScrollPane;
import java.awt.Scrollbar;
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;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class readerwriter extends JFrame {
private TextArea textarea;
private JButton bt1;
private JButton bt2;
private ScrollPane scroll;
public readerwriter()
{
scroll=new ScrollPane();
JPanel panel2=new JPanel();
textarea=new TextArea(10,8);
bt1=new JButton("读取");
bt2=new JButton("写入");
scroll.add(textarea);
panel2.add(bt1);
scroll.add(textarea);
panel2.add(bt2);
this.setLayout(new BorderLayout());
panel2.setLayout(new FlowLayout());
this.add(scroll,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bt1.addActionListener(new ActionListener()//读取
{
public void actionPerformed(ActionEvent e) {
try {
FileReader reader=new FileReader("F:\\textinfo\\my.txt");
char byt[]=new char[1024];
int len;
try {
len = reader.read(byt);
textarea.setText(new String(byt,0,len));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
});
bt2.addActionListener(new ActionListener(){//写入
@Override
public void actionPerformed(ActionEvent e) {
File file=new File("F:\\textinfo\\my.txt");
try {
FileWriter input=new FileWriter(file);
String str1=textarea.getText();
input.write(str1);
input.flush();
input.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public static void main(String[] args) {
new readerwriter();
}
}