import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class fileoperator implements ActionListener{
JFrame jf=new JFrame("fileoperator");
JTextArea ta=new JTextArea(10,10);
fileoperator(){
JPanel p=new JPanel();
JButton open=new JButton("读文件");
JButton save=new JButton("写文件");
jf.add(ta);jf.add(open);jf.add(save);jf.add(p,"South");
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser();
jfc.showOpenDialog(jf);
File file=jfc.getSelectedFile();
try {
FileInputStream fis=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
String s;
ta.setText("");
while((s=br.readLine())!=null)
ta.append(s+"\n");
br.close();
}catch(Exception ee) {
System.out.println("文件不存在!");
}
}
});
save.addActionListener(this);
jf.setBounds(200, 200, 200,200);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser();
jfc.showSaveDialog(jf);
File file=jfc.getSelectedFile();
try {
FileOutputStream fos=new FileOutputStream(file);
OutputStreamWriter osw=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(osw);
String s;
bw.write(ta.getText());
bw.close();
}catch(Exception ee){
System.out.println("没有存储空间!");
}
}
}
class FiletoUnicode{
public static void main(String []ppp) {
new fileoperator();
}
}