Write content to txt file (overwrite and rewrite at the end + FileOutputStream and FileWriter) (forward: https://blog.csdn.net/bestcxx/article/details/51381460)

!!!!

Read the contents of a txt file

[java] view plain copy
  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileReader;  
  4.   
  5. /** 
  6.  * May 11, 2016 
  7.  * This class simulates the process of reading content from a txt file 
  8.  * @author WuJieJecket 
  9.  * 
  10.  */  
  11. public class PrintFromTxt {  
  12.     public static void main(String[] args) throws Exception {  
  13.       
  14.         //read all file contents  
  15.         File file = new File("d:\\a\\123.txt");  
  16.         StringBuilder sb = new StringBuilder();  
  17.         String s ="";  
  18.         BufferedReader br = new BufferedReader(new FileReader(file));  
  19.   
  20.         while( (s = br.readLine()) != null) {  
  21.         sb.append(s + "\n");  
  22.         }  
  23.   
  24.         br.close();  
  25.         String str = sb.toString();  
  26.         System.out.println(str);  
  27.           
  28.     }  
  29.   
  30. }  



 

! ! ! write to file - if the specified file does not exist, it will be created

Use if in the main method to judge whether to run the test method. There are four test methods. Both FileOutputStream and FileWriter are written to txt files. The difference is that the latter is written in the cache first, and the cache is cleared or the cache is full. The content is put into txt.

[java] view plain copy
  1. import java.io.FileNotFoundException;  
  2. import java.io.FileOutputStream;  
  3. import java.io.FileWriter;  
  4. import java.io.IOException;  
  5.   
  6. /** 
  7.  * May 11, 2016 
  8.  * This class simulates the process of writing strings to txt files 
  9.  * @author WuJieJecket 
  10.  * 
  11.  */  
  12. public class WriteToTxt {  
  13.      public static void main(String[] args) throws FileNotFoundException {  
  14.            
  15.          //Content to be written to the file \r is a newline  
  16.          String str1= "\rZhang San 1 0 3000\rLi Si 1 1 5000\rWang Wu 1 0 4000";  
  17.          String str2= "\rZhang San 2 0 3000\rLi Si 2 1 5000\rWang Wu 2 0 4000";  
  18.          String str3= "\rZhang San 3 0 3000\rLi Si 3 1 5000\rWang Wu 3 0 4000";  
  19.          String str4= "\rZhang San 4 0 3000\rLi Si 4 1 5000\rWang Wu 4 0 4000";  
  20.            
  21.          WriteToTxt wtt=new WriteToTxt();  
  22.           
  23.          //FileWriter-cache write-character-overwrite rewrite  
  24.          if(!true){  
  25.              wtt.FileWriterTest(str1);  
  26.          }  
  27.           
  28.          //FileOutputStream-direct write-byte-overwrite rewrite  
  29.          if(!true){  
  30.              wtt.FileOutputStreamTest(str2);  
  31.          }  
  32.            
  33.          //FileWriter-cache write-character-end continuation  
  34.          if(!true){  
  35.              wtt.FileWriterTest2(str3);  
  36.          }  
  37.           
  38.          //FileOutputStream-direct write-byte-end continuation  
  39.          if(!true){  
  40.              wtt.FileOutputStreamTest2(str4);  
  41.          }  
  42.            
  43.            
  44.         }  
  45.        
  46.      /* 
  47.       * write file 
  48.       * Overwrite 
  49.       * FileWriter 
  50.       * Write in the cache first, need to flush 
  51.       * character eg:1,2,a,b 
  52.       */  
  53.      public void FileWriterTest(String str){  
  54.          FileWriter writer;  
  55.             try {  
  56.                // writer = new FileWriter("/home/1.txt");  
  57.                 writer = new FileWriter("d:\\a\\1231.txt");  
  58.                 writer.write(str);  
  59.                 writer.flush();      //This one is more important, it is the process of clearing the cache, and then the information to be written is written to the txt file  
  60.                 writer.close();  
  61.             } catch (IOException e) {  
  62.                 e.printStackTrace ();  
  63.             }  
  64.      }  
  65.        
  66.      /* 
  67.       * write file 
  68.       * Override rewrite  
  69.       *FileOutputStream 
  70.       *Write directly to the file, there is no cache 
  71.       *byte byte 
  72.       */  
  73.      public void FileOutputStreamTest(String str){  
  74.          FileOutputStream fos;  
  75.          try {  
  76.             fos=new FileOutputStream("d:\\a\\1232.txt");  
  77.             fos.write(str.getBytes());  
  78.             fos.close();  
  79.               
  80.         } catch (FileNotFoundException e) {  
  81.             // TODO Auto-generated catch block  
  82.             e.printStackTrace ();  
  83.         } catch (IOException e) {  
  84.             // TODO Auto-generated catch block  
  85.             e.printStackTrace ();  
  86.         }  
  87.      }  
  88.        
  89.        
  90.        
  91.        
  92.      /* 
  93.       * write file 
  94.       * Continued at the end 
  95.       * FileWriter 
  96.       * Write in the cache first, need to flush 
  97.       * character eg:1,2,a,b 
  98.       */  
  99.      public void FileWriterTest2(String str){  
  100.          FileWriter writer;  
  101.             try {  
  102.                // writer = new FileWriter("/home/1.txt");  
  103.                 writer = new FileWriter("d:\\a\\1233.txt",true);  
  104.                 writer.write(str);  
  105.                 writer.flush();      //This one is more important, it is the process of clearing the cache, and then the information to be written is written to the txt file  
  106.                 writer.close();  
  107.             } catch (IOException e) {  
  108.                 e.printStackTrace ();  
  109.             }  
  110.      }  
  111.        
  112.      /* 
  113.       * write file 
  114.       *Continued at the end 
  115.       *FileOutputStream 
  116.       *Write directly to the file, there is no cache 
  117.       *byte byte 
  118.       */  
  119.      public void FileOutputStreamTest2(String str){  
  120.          FileOutputStream fos;  
  121.          try {  
  122.             fos=new FileOutputStream("d:\\a\\1234.txt",true);  
  123.             fos.write(str.getBytes());  
  124.             fos.close();  
  125.               
  126.         } catch (FileNotFoundException e) {  
  127.             // TODO Auto-generated catch block  
  128.             e.printStackTrace ();  
  129.         } catch (IOException e) {  
  130.             // TODO Auto-generated catch block  
  131.             e.printStackTrace ();  
  132.         }  
  133.      }  
  134.   
  135.        
  136. }  

 

 

txt read by line and column

http://blog.csdn.net/bestcxx/article/details/65446489

 

Finally, a packaged method written by myself is attached, which can be used directly and will not overwrite the original file (ie, continue at the end)

package com.zhaowu.renwu2;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtil {
    public static void toFile (String content) {
        File file = null;
        FileWriter fw = null;
        file = new File("/home/acer/桌面/aaa");
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fw = new FileWriter(file,true);
            fw.write(content); // Copy the content to the file 
            fw.flush();
        } catch (IOException e) {
            e.printStackTrace ();
        }finally{
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace ();
                }
            }
        }
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325949418&siteId=291194637