• Java学习-014-文本文件写入实例源代码(两种写入方式)


    此文源码主要为应用 Java 读取文本文件内容实例的源代码。若有不足之处,敬请大神指正,不胜感激!

    第一种:文本文件写入,若文件存在则删除原文件,并重新创建文件。源代码如下所示:

     1     /**
     2      * @function 文本文件操作:写入数据
     3      * 
     4      * @author Aaron.ffp
     5      * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java txtWrite, 2015-2-2 21:03:53 Exp $
     6      * 
     7      * @param filename     :文本文件全路径
     8      * @param encoding     :编码格式
     9      * @param fileContent  :写入内容
    10      * 
    11      * @return boolean 写入成功返回true
    12      * 
    13      * @throws IOException
    14      */
    15     public boolean txtWrite(String filename, String encoding, String fileContent){
    16         // 先读取源文件内容, 再进行写入操作
    17         boolean flag = false;
    18         
    19         FileInputStream fis = null;
    20         InputStreamReader isr = null;
    21         BufferedReader br = null;
    22         FileOutputStream fos = null;
    23         PrintWriter pw = null;
    24         
    25         /* 创建文件(删除旧文件) */
    26         if (!this.createFile(filename)) {
    27             return flag;
    28         }
    29         
    30         try{
    31             File file = new File(filename); // 文件路径
    32             
    33             if(file.isFile()){
    34                 // 将文件读入输入流
    35                 fis = new FileInputStream(file);
    36                 isr = new InputStreamReader(fis);
    37                 br = new BufferedReader(isr);
    38                 
    39                 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), encoding);
    40                 BufferedWriter bw = new BufferedWriter(osw);
    41                 bw.write(fileContent);
    42                 bw.close();
    43                 
    44                 flag = true;
    45             }else{
    46                 this.message = "文件全路径非法。当前文件全路径为:" + filename;
    47                 this.logger.warn(this.message);
    48             }
    49         }catch(Exception ioe){
    50             this.message = "写文件{" + filename + "}内容出错。" + ioe.getMessage();
    51             this.logger.error(this.message);
    52         }finally{
    53             try {
    54                 if(pw != null){
    55                     pw.close();
    56                 }
    57                 if(fos != null){
    58                     fos.close();
    59                 }
    60                 if(br != null){
    61                     br.close();
    62                 }
    63                 if(fis != null){
    64                     fis.close();
    65                 }
    66             } catch (Exception e) {
    67                 this.message = e.getMessage();
    68                 this.logger.error(this.message);
    69             }
    70         }
    71         
    72         return flag;
    73     }
    文本文件覆盖写入源代码

    测试源码如下所示:

     1     /**
     2      * 测试:创建文件-FileUtils.textWrite(String, String, String)
     3      * 
     4      * @author Aaron.ffp
     5      * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_txtWrite, 2015-2-2 22:09:51 Exp $
     6      */
     7     @Test
     8     public void test_txtWrite() {
     9         this.message = "
    
    
    测试:FileUtils.textWrite(String, String, String)";
    10         this.logger.debug(this.message);
    11         
    12         try{
    13             this.fu = new FileUtils();
    14             this.txtfileWrite = this.constantslist.PARAFILEPATH.get("OUT") + "txtfileWrite.txt"; // 文件名
    15             this.message = "写入文件路径为:" + this.txtfileWrite;
    16             
    17             this.fu.createFile(this.txtfileWrite);
    18             
    19             for(int i = 0; i < 10; i++){
    20                 this.fu.txtWrite(this.txtfileWrite, "UTF-8", "显示追加的信息:" + i);
    21             }
    22             
    23             LinkedList<String> contentlist = this.fu.txtRead(this.txtfileWrite, "UTF-8");
    24             
    25             if(contentlist.size() > 0){
    26                 for(int i = 0; i < contentlist.size(); i++){
    27                     this.logger.debug(contentlist.get(i));
    28                 }
    29             }
    30         }catch(Exception ioe){
    31             this.message = ioe.getMessage();
    32             this.logger.error(this.message);
    33         }
    34     }
    测试:文本文件覆盖写入

    第二种:文本文件写入,依据用户的提示指令是否删除已存在的原文件。若删除原文件,则同第一种;若不删除原文件,则在原文件末尾追加内容。源代码如下所示:

     1     /**
     2      * @function 文本文件操作:写入数据
     3      * 
     4      * @author Aaron.ffp
     5      * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java txtWrite, 2015-2-2 21:10:53 Exp $
     6      * 
     7      * @param filename     :文本文件全路径
     8      * @param delete       : 是否删除旧文件
     9      * @param encoding     :编码格式
    10      * @param fileContent  :写入内容
    11      * 
    12      * @return boolean 写入成功返回true
    13      * 
    14      * @throws IOException
    15      */
    16     public boolean txtWrite(String filename, boolean delete, String encoding, String fileContent){
    17         // 先读取源文件内容, 再进行写入操作
    18         boolean flag = false;
    19         String temp = "";
    20         String filecontent = "";
    21         
    22         FileInputStream fis = null;
    23         InputStreamReader isr = null;
    24         BufferedReader br = null;
    25         FileOutputStream fos = null;
    26         PrintWriter pw = null;
    27         
    28         /* 文件创建:若删除源文件,则为覆盖写入;若不删除原文件,则为追加写入 */
    29         if (!this.createFile(filename, delete)) {
    30             return flag;
    31         }
    32         
    33         try{
    34             File file = new File(filename); // 文件路径
    35             
    36             if(file.isFile()){
    37                 // 将文件读入输入流
    38                 fis = new FileInputStream(file);
    39                 isr = new InputStreamReader(fis);
    40                 br = new BufferedReader(isr);
    41                 
    42                 // 获取文件原有的内容
    43                 for(; (temp = br.readLine()) != null;){
    44                     filecontent += temp + this.constantslist.LINESEPARATOR;
    45                 }
    46                 
    47                 fileContent = filecontent + fileContent;
    48                 
    49                 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), encoding);
    50                 BufferedWriter bw = new BufferedWriter(osw);
    51                 bw.write(fileContent);
    52                 bw.close();
    53                 
    54                 flag = true;
    55             }else{
    56                 this.message = "文件全路径非法。当前文件全路径为:" + filename;
    57                 this.logger.warn(this.message);
    58             }
    59         }catch(Exception ioe){
    60             this.message = "写文件{" + filename + "}内容出错。" + ioe.getMessage();
    61             this.logger.error(this.message);
    62         }finally{
    63             try {
    64                 if(pw != null){
    65                     pw.close();
    66                 }
    67                 if(fos != null){
    68                     fos.close();
    69                 }
    70                 if(br != null){
    71                     br.close();
    72                 }
    73                 if(fis != null){
    74                     fis.close();
    75                 }
    76             } catch (Exception e) {
    77                 this.message = e.getMessage();
    78                 this.logger.error(this.message);
    79             }
    80         }
    81         
    82         return flag;
    83     }
    文本文件写入源代码:

    测试源码如下所示:

     1     /**
     2      * 测试:FileUtils.textWrite(String, boolean, String, String)
     3      * 
     4      * @author Aaron.ffp
     5      * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_txtWriteAppend, 2015-2-2 22:19:51 Exp $
     6      */
     7     @Test
     8     public void test_txtWriteAppend() {
     9         this.message = "
    
    
    测试:FileUtils.textWrite(String, boolean, String, String)";
    10         this.logger.debug(this.message);
    11         
    12         try{
    13             this.fu = new FileUtils();
    14             this.txtfileWrite = this.constantslist.PARAFILEPATH.get("OUT") + "txtfileWrite-append.txt"; // 文件名
    15             this.message = "写入文件路径为:" + this.txtfileWrite;
    16             
    17             this.fu.createFile(this.txtfileWrite, false);
    18             
    19             for(int i = 0; i < 10; i++){
    20                 this.fu.txtWrite(this.txtfileWrite, false, "UTF-8", "显示追加的信息:" + i);
    21             }
    22             
    23             LinkedList<String> contentlist = this.fu.txtRead(this.txtfileWrite, "UTF-8");
    24             
    25             if(contentlist.size() > 0){
    26                 for(int i = 0; i < contentlist.size(); i++){
    27                     this.logger.debug(contentlist.get(i));
    28                 }
    29             }
    30         }catch(Exception ioe){
    31             this.message = ioe.getMessage();
    32             this.logger.error(this.message);
    33         }
    34     }
    测试:文本文件写入

    至此, Java学习-014-文本文件写入实例源代码(两种写入方式) 顺利完结,希望此文能够给初学 Java 的您一份参考。

    最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

     

  • 相关阅读:
    Linux Bash
    grep 及正则表达式
    Linux 文件系统
    Linux 操作系统基础
    常见的磁盘I/O和网络I/O优化技巧
    NIO的工作方式
    网络I/O 工作机制
    spring Boot环境下dubbo+zookeeper的一个基础讲解与示例
    深入分析Java I/O 工作机制
    CDN工作机制和负载均衡
  • 原文地址:https://www.cnblogs.com/fengpingfan/p/4337384.html
Copyright © 2020-2023  润新知