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

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

     /**
* @function 文本文件操作:写入数据
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java txtWrite, 2015-2-2 21:03:53 Exp $
*
* @param filename :文本文件全路径
* @param encoding :编码格式
* @param fileContent :写入内容
*
* @return boolean 写入成功返回true
*
* @throws IOException
*/
public boolean txtWrite(String filename, String encoding, String fileContent){
// 先读取源文件内容, 再进行写入操作
boolean flag = false; FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null; /* 创建文件(删除旧文件) */
if (!this.createFile(filename)) {
return flag;
} try{
File file = new File(filename); // 文件路径 if(file.isFile()){
// 将文件读入输入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), encoding);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(fileContent);
bw.close(); flag = true;
}else{
this.message = "文件全路径非法。当前文件全路径为:" + filename;
this.logger.warn(this.message);
}
}catch(Exception ioe){
this.message = "写文件{" + filename + "}内容出错。" + ioe.getMessage();
this.logger.error(this.message);
}finally{
try {
if(pw != null){
pw.close();
}
if(fos != null){
fos.close();
}
if(br != null){
br.close();
}
if(fis != null){
fis.close();
}
} catch (Exception e) {
this.message = e.getMessage();
this.logger.error(this.message);
}
} return flag;
}

文本文件覆盖写入源代码

测试源码如下所示:

     /**
* 测试:创建文件-FileUtils.textWrite(String, String, String)
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_txtWrite, 2015-2-2 22:09:51 Exp $
*/
@Test
public void test_txtWrite() {
this.message = "\n\n\n测试:FileUtils.textWrite(String, String, String)";
this.logger.debug(this.message); try{
this.fu = new FileUtils();
this.txtfileWrite = this.constantslist.PARAFILEPATH.get("OUT") + "txtfileWrite.txt"; // 文件名
this.message = "写入文件路径为:" + this.txtfileWrite; this.fu.createFile(this.txtfileWrite); for(int i = 0; i < 10; i++){
this.fu.txtWrite(this.txtfileWrite, "UTF-8", "显示追加的信息:" + i);
} LinkedList<String> contentlist = this.fu.txtRead(this.txtfileWrite, "UTF-8"); if(contentlist.size() > 0){
for(int i = 0; i < contentlist.size(); i++){
this.logger.debug(contentlist.get(i));
}
}
}catch(Exception ioe){
this.message = ioe.getMessage();
this.logger.error(this.message);
}
}

测试:文本文件覆盖写入

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

     /**
* @function 文本文件操作:写入数据
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java txtWrite, 2015-2-2 21:10:53 Exp $
*
* @param filename :文本文件全路径
* @param delete : 是否删除旧文件
* @param encoding :编码格式
* @param fileContent :写入内容
*
* @return boolean 写入成功返回true
*
* @throws IOException
*/
public boolean txtWrite(String filename, boolean delete, String encoding, String fileContent){
// 先读取源文件内容, 再进行写入操作
boolean flag = false;
String temp = "";
String filecontent = ""; FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null; /* 文件创建:若删除源文件,则为覆盖写入;若不删除原文件,则为追加写入 */
if (!this.createFile(filename, delete)) {
return flag;
} try{
File file = new File(filename); // 文件路径 if(file.isFile()){
// 将文件读入输入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr); // 获取文件原有的内容
for(; (temp = br.readLine()) != null;){
filecontent += temp + this.constantslist.LINESEPARATOR;
} fileContent = filecontent + fileContent; OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), encoding);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(fileContent);
bw.close(); flag = true;
}else{
this.message = "文件全路径非法。当前文件全路径为:" + filename;
this.logger.warn(this.message);
}
}catch(Exception ioe){
this.message = "写文件{" + filename + "}内容出错。" + ioe.getMessage();
this.logger.error(this.message);
}finally{
try {
if(pw != null){
pw.close();
}
if(fos != null){
fos.close();
}
if(br != null){
br.close();
}
if(fis != null){
fis.close();
}
} catch (Exception e) {
this.message = e.getMessage();
this.logger.error(this.message);
}
} return flag;
}

文本文件写入源代码:

测试源码如下所示:

     /**
* 测试:FileUtils.textWrite(String, boolean, String, String)
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_txtWriteAppend, 2015-2-2 22:19:51 Exp $
*/
@Test
public void test_txtWriteAppend() {
this.message = "\n\n\n测试:FileUtils.textWrite(String, boolean, String, String)";
this.logger.debug(this.message); try{
this.fu = new FileUtils();
this.txtfileWrite = this.constantslist.PARAFILEPATH.get("OUT") + "txtfileWrite-append.txt"; // 文件名
this.message = "写入文件路径为:" + this.txtfileWrite; this.fu.createFile(this.txtfileWrite, false); for(int i = 0; i < 10; i++){
this.fu.txtWrite(this.txtfileWrite, false, "UTF-8", "显示追加的信息:" + i);
} LinkedList<String> contentlist = this.fu.txtRead(this.txtfileWrite, "UTF-8"); if(contentlist.size() > 0){
for(int i = 0; i < contentlist.size(); i++){
this.logger.debug(contentlist.get(i));
}
}
}catch(Exception ioe){
this.message = ioe.getMessage();
this.logger.error(this.message);
}
}

测试:文本文件写入

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

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

Java学习-014-文本文件写入实例源代码(两种写入方式)的更多相关文章

  1. Java学习-019-Properties 文件读取实例源代码

    在这几天的学习过程中,有开发的朋友告知我,每个编程语言基本都有相应的配置文件支持类,像 Python 编程语言中支持的 ini 文件及其对应的配置文件读取类 ConfigParse,通过这个类,用户可 ...

  2. Java学习-016-CSV 文件读取实例源代码

    上文(CSV文件写入)讲述了日常自动化测试过程中将测试数据写入 CSV 文件的源码,此文主要讲述如何从 CSV 文件获取测试过程中所需的参数化数据.敬请各位小主参阅,若有不足之处,敬请大神指正,不胜感 ...

  3. Java学习-017-EXCEL 文件读取实例源代码

    众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...

  4. Java多线程13:读写锁和两种同步方式的对比

    读写锁ReentrantReadWriteLock概述 大型网站中很重要的一块内容就是数据的读写,ReentrantLock虽然具有完全互斥排他的效果(即同一时间只有一个线程正在执行lock后面的任务 ...

  5. Spring学习之Spring与Mybatis的两种整合方式

    本机使用IDEA 2020.1.MySql 8.0.19,通过Maven进行构建 环境准备 导入maven依赖包 <dependencies> <dependency> < ...

  6. Java Callable接口与Future接口的两种使用方式

    Java Callable.Future的两种使用方式Callable+Futurepublic class Test { public static void main(String[] args) ...

  7. RabbitMQ学习第二记:工作队列的两种分发方式,轮询分发(Round-robin)和 公平分发(Fair dispatch)

    1.什么是RabbitMQ工作队列 我们在应用程序使用消息系统时,一般情况下生产者往队列里插入数据时速度是比较快的,但是消费者消费数据往往涉及到一些业务逻辑处理导致速度跟不上生产者生产数据.因此如果一 ...

  8. java多线程总结一:线程的两种创建方式及比较

    1.线程的概念:线程(thread)是指一个任务从头至尾的执行流,线程提供一个运行任务的机制,对于java而言,一个程序中可以并发的执行多个线程,这些线程可以在多处理器系统上同时运行.当程序作为一个应 ...

  9. java多线程总结一:线程的两种创建方式及优劣比较

    1.通过实现Runnable接口线程创建 (1).定义一个类实现Runnable接口,重写接口中的run()方法.在run()方法中加入具体的任务代码或处理逻辑. (2).创建Runnable接口实现 ...

随机推荐

  1. Codeforces Round #199 (Div. 2) A Xenia and Divisors

    注意题目的数字最大是7 而能整除的只有 1,2,3,4,6,故构成的组合只能是1,2,4 或1,2,6或1,3,6,故分别统计1,2,3,4,6的个数,然后再分配 #include <iostr ...

  2. 【BZOJ】1189: [HNOI2007]紧急疏散evacuate(二分+bfs+网络流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1189 表示完全不会QAQ.... 于是膜拜题解orz 二分时间........... 于是转换成判定 ...

  3. WebRTC手记之WebRtcVideoEngine2模块

    转载请注明出处:http://www.cnblogs.com/fangkm/p/4401143.html 终于讲到视频数据的编码发送模块了,不容易.总体来说也看了不少时间WebRTC的源码了,最大的感 ...

  4. C#开发MySQL数据库程序时需要注意的几点

    一:引用MySQL使用基于Parameter方式代码,总是提示:“Column '列名'cannot be null”解决 MySQL使用基于Parameter方式代码,总是提示:“Column '列 ...

  5. 使 SortList 实现重复键排序

    SortList 默认对按Key来排序,且Key值不能重复,但有时可能需要用有重复值的Key来排序,以下是实现方式: 1.对强类型:以float为例 #region 使SortList能对重复键排序 ...

  6. Maya Shortcuts 常用快捷键

    快捷键 功能解释 工具操作 enter 完成当前操作 ~ 终止当前操作 insert 插入工具编辑模式 w 移动工具 e 旋转工具 r 缩放工具 y 非固定排布工具 shift+Q 选择工具,(切换到 ...

  7. Html - Footer

    通用的Footer代码片段 <style> #footer { padding: 20px; text-align: center; background-color: #666; bor ...

  8. php文件以二进制形式上传并放入到数据库中

    conn.php: <?php $id=mysql_connect('localhost','root','root'); mysql_select_db("db_database12 ...

  9. 使用 matlab 产生GK101任意波数据文件的方法

    一.引言 MATLAB是由美国mathworks公司发布的主要面对科学计算.可视化以及交互式程序设计的高科技计算环境.它不但包含高效的数值计算.数据处理能力,而且简单易用,是工程师日常研发过程中不可缺 ...

  10. PHP 开发 APP 接口 学习笔记与总结 - JSON 方式封装通信接口

    1.通信数据的标准格式 ( JSON ),包括: code:状态码(200,400等) message:提示信息(例如:数据返回成功.邮箱格式错误等) data:返回数据 2.JSON 方式封装通信接 ...