• 向txt文件中写入内容(覆盖重写与在末尾续写+FileOutputStream与FileWriter)(转发:https://blog.csdn.net/bestcxx/article/details/51381460)


    !!!!

    读取txt文件中的内容

    1. import java.io.BufferedReader;  
    2. import java.io.File;  
    3. import java.io.FileReader;  
    4.   
    5. /** 
    6.  * 2016年05月11日 
    7.  * 本类模拟从txt文件读取内容的过程 
    8.  * @author WuJieJecket 
    9.  * 
    10.  */  
    11. public class PrintFromTxt {  
    12.     public static void main(String[] args) throws Exception {  
    13.       
    14.         //读取全部文件内容  
    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 + " ");  
    22.         }  
    23.   
    24.         br.close();  
    25.         String str = sb.toString();  
    26.         System.out.println(str);  
    27.           
    28.     }  
    29.   
    30. }  



    !!!向文件中写入内容-如果指定文件不存在,会被创建

    在main方法中使用if做判断,是否运行测试方法,测试方法有四个,FileOutputStream与FileWriter都是写入到txt文件,区别是后者先写在缓存,清缓存或者缓存已满才会最终把内容放入到txt中。

    1. import java.io.FileNotFoundException;  
    2. import java.io.FileOutputStream;  
    3. import java.io.FileWriter;  
    4. import java.io.IOException;  
    5.   
    6. /** 
    7.  * 2016年05月11日 
    8.  * 本类模拟将字符串写入到txt文件的过程 
    9.  * @author WuJieJecket 
    10.  * 
    11.  */  
    12. public class WriteToTxt {  
    13.      public static void main(String[] args) throws FileNotFoundException {  
    14.            
    15.          //要写入到文件中的内容   是换行  
    16.          String str1=" 张三1 0 3000 李四1 1 5000 王五1 0 4000";  
    17.          String str2=" 张三2 0 3000 李四2 1 5000 王五2 0 4000";  
    18.          String str3=" 张三3 0 3000 李四3 1 5000 王五3 0 4000";  
    19.          String str4=" 张三4 0 3000 李四4 1 5000 王五4 0 4000";  
    20.            
    21.          WriteToTxt wtt=new WriteToTxt();  
    22.           
    23.          //FileWriter-缓存写入-字符-覆盖重写  
    24.          if(!true){  
    25.              wtt.FileWriterTest(str1);  
    26.          }  
    27.           
    28.          //FileOutputStream-直接写入-字节-覆盖重写  
    29.          if(!true){  
    30.              wtt.FileOutputStreamTest(str2);  
    31.          }  
    32.            
    33.          //FileWriter-缓存写入-字符-末尾续接  
    34.          if(!true){  
    35.              wtt.FileWriterTest2(str3);  
    36.          }  
    37.           
    38.          //FileOutputStream-直接写入-字节-末尾续接  
    39.          if(!true){  
    40.              wtt.FileOutputStreamTest2(str4);  
    41.          }  
    42.            
    43.            
    44.         }  
    45.        
    46.      /* 
    47.       * 写文件 
    48.       * 覆盖重写 
    49.       * FileWriter 
    50.       * 先写在缓存中,需要flush 
    51.       * 字符 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();     //这一个比较重要,是清理缓存的过程,之后需要写入的信息被写到txt文件中  
    60.                 writer.close();  
    61.             } catch (IOException e) {  
    62.                 e.printStackTrace();  
    63.             }  
    64.      }  
    65.        
    66.      /* 
    67.       *写文件 
    68.       *覆盖重写  
    69.       *FileOutputStream 
    70.       *直接写入到文件中,不存在缓存 
    71.       *字节 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.       * 写文件 
    94.       * 末尾续接 
    95.       * FileWriter 
    96.       * 先写在缓存中,需要flush 
    97.       * 字符 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();     //这一个比较重要,是清理缓存的过程,之后需要写入的信息被写到txt文件中  
    106.                 writer.close();  
    107.             } catch (IOException e) {  
    108.                 e.printStackTrace();  
    109.             }  
    110.      }  
    111.        
    112.      /* 
    113.       *写文件 
    114.       *末尾续接 
    115.       *FileOutputStream 
    116.       *直接写入到文件中,不存在缓存 
    117.       *字节 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的按行、列读

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

    最后附上自己写的一个封装好的方法,可以直接使用,不会覆盖原文件(即末尾续接)

    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);//向文件中复制内容
                fw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(fw != null){
                    try {
                        fw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
  • 相关阅读:
    maven的安装教程
    webstorm的中文教程和技巧分享
    WebStorm
    grunt配置任务
    grunt快速入门
    CSS简介
    浅介HTML DOM
    【转】计算机是如何启动的?
    【转】深入理解C++中public、protected及private用法
    【转】VS2013动态库文件的创建及其使用详解
  • 原文地址:https://www.cnblogs.com/sutao/p/9012318.html
Copyright © 2020-2023  润新知