[Java] java对文件的读取和保存操作 →→→→→进入此内容的聊天室

来自 , 2019-09-08, 写在 Java, 查看 99 次.
URL http://www.code666.cn/view/4fc28b70
  1. import java.awt.FlowLayout;
  2. import java.awt.TextArea;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.BufferedReader;
  6. import java.io.BufferedWriter;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileReader;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.text.SimpleDateFormat;
  13.  
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JPanel;
  17.  
  18.  
  19. public class ReadAndWriteText extends JFrame implements ActionListener{
  20.     TextArea text=new TextArea(15,30);
  21.     JButton Read=new JButton("读取文件");
  22.     JButton Save=new JButton("保存文件");
  23.     JPanel btnpnl=new JPanel();
  24.     File file=new File("text.txt");
  25.     ReadAndWriteText(){
  26.          btnpnl.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
  27.          btnpnl.add(Read);
  28.          btnpnl.add(Save);
  29.          Read.addActionListener(this);
  30.          Save.addActionListener(this);
  31.          this.setLayout(new FlowLayout());
  32.          this.add(btnpnl);
  33.          this.add(text);
  34.          this.setBounds(450, 150, 300, 400);
  35.          this.setVisible(true);
  36.          this.validate();
  37.          this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  38.     }
  39.     private void save()
  40.     {
  41.        
  42.         System.out.println(file.getAbsolutePath());//获得绝对路径
  43.                 try {
  44.                         FileWriter fw=new FileWriter(file);
  45.                         BufferedWriter bfw=new BufferedWriter(fw);
  46.                         String str=new String();
  47.                         str=text.getText();
  48.                         bfw.write(str, 0, str.length());
  49.                         bfw.flush();
  50.                 } catch (IOException e1) {
  51.                         // TODO Auto-generated catch block
  52.                         e1.printStackTrace();
  53.                 }
  54.        
  55.     }
  56.     private void read() {
  57.                 // TODO Auto-generated method stub
  58.         try {
  59.                         FileReader fr=new FileReader(file);
  60.                         BufferedReader bfr=new BufferedReader(fr);
  61.                         String str=new String();
  62.                         while((str=bfr.readLine())!=null){
  63.                                 text.append(str+"\n");
  64.                         }
  65.                 } catch (IOException e) {
  66.                         // TODO Auto-generated catch block
  67.                         e.printStackTrace();
  68.                 }
  69.                
  70.         }
  71.     @Override
  72.         public void actionPerformed(ActionEvent e) {
  73.                 // TODO Auto-generated method stub
  74.                 if(Save==e.getSource()){
  75.                         save();
  76.                         System.out.print("writing");
  77.                 }
  78.                 if(Read==e.getSource()){
  79.                         read();
  80.                         System.out.print("reading");
  81.                 }
  82.                
  83.         }
  84.    
  85.         public static void main(String []args){
  86.             new ReadAndWriteText();
  87.            // System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ssss")).format(time2));
  88.     }
  89. }
  90.  
  91. //java/1347

回复 "java对文件的读取和保存操作"

这儿你可以回复上面这条便签

captcha