Java学习-014-文本文件写入实例源代码(两种写入方式)
此文源码主要为应用 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-文本文件写入实例源代码(两种写入方式)的更多相关文章
- Java学习-019-Properties 文件读取实例源代码
在这几天的学习过程中,有开发的朋友告知我,每个编程语言基本都有相应的配置文件支持类,像 Python 编程语言中支持的 ini 文件及其对应的配置文件读取类 ConfigParse,通过这个类,用户可 ...
- Java学习-016-CSV 文件读取实例源代码
上文(CSV文件写入)讲述了日常自动化测试过程中将测试数据写入 CSV 文件的源码,此文主要讲述如何从 CSV 文件获取测试过程中所需的参数化数据.敬请各位小主参阅,若有不足之处,敬请大神指正,不胜感 ...
- Java学习-017-EXCEL 文件读取实例源代码
众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...
- Java多线程13:读写锁和两种同步方式的对比
读写锁ReentrantReadWriteLock概述 大型网站中很重要的一块内容就是数据的读写,ReentrantLock虽然具有完全互斥排他的效果(即同一时间只有一个线程正在执行lock后面的任务 ...
- Spring学习之Spring与Mybatis的两种整合方式
本机使用IDEA 2020.1.MySql 8.0.19,通过Maven进行构建 环境准备 导入maven依赖包 <dependencies> <dependency> < ...
- Java Callable接口与Future接口的两种使用方式
Java Callable.Future的两种使用方式Callable+Futurepublic class Test { public static void main(String[] args) ...
- RabbitMQ学习第二记:工作队列的两种分发方式,轮询分发(Round-robin)和 公平分发(Fair dispatch)
1.什么是RabbitMQ工作队列 我们在应用程序使用消息系统时,一般情况下生产者往队列里插入数据时速度是比较快的,但是消费者消费数据往往涉及到一些业务逻辑处理导致速度跟不上生产者生产数据.因此如果一 ...
- java多线程总结一:线程的两种创建方式及比较
1.线程的概念:线程(thread)是指一个任务从头至尾的执行流,线程提供一个运行任务的机制,对于java而言,一个程序中可以并发的执行多个线程,这些线程可以在多处理器系统上同时运行.当程序作为一个应 ...
- java多线程总结一:线程的两种创建方式及优劣比较
1.通过实现Runnable接口线程创建 (1).定义一个类实现Runnable接口,重写接口中的run()方法.在run()方法中加入具体的任务代码或处理逻辑. (2).创建Runnable接口实现 ...
随机推荐
- 【BZOJ】1002: [FJOI2007]轮状病毒(DP+规律+高精度)
http://www.lydsy.com/JudgeOnline/problem.php?id=1002 其实我还是看题解的,而且看了题解也没明白那公式怎么来的T_T,先水过了先把....以后研究一下 ...
- hiho 毁灭者问题
描述 在 Warcraft III 之冰封王座中,毁灭者是不死族打三本后期时的一个魔法飞行单位. 毁灭者的核心技能之一,叫做魔法吸收(Absorb Mana): 现在让我们来考虑下面的问题: 假设你拥 ...
- Ubuntu 12.04 下安装 VirtualBox 及虚拟机winxp的安装
参考文档: http://wenku.baidu.com/view/a51ac26c9b6648d7c1c746d7.html 1.首先,先去官网(http://www.virtualbox.org) ...
- FireFox火狐浏览器与IE兼容问题 - 透明滤镜 DIV滚动条
问题一:最简单的鼠标移过手变型的css要改了 cursor:pointer;/*FireFox(火狐)不支持cursor:hand*/ dw8下面自动出来的也没有hand这个属性了,标准的是point ...
- Javascript Math ceil()、floor()、round()三个函数的区别
Round是四舍五入的...Ceiling是向上取整..float是向下取整. ceil():将小数部分一律向整数部分进位. 如: Math.ceil(12.2)//返回13 Math.ceil(12 ...
- CSS中a标签样式的“爱恨”原则
CSS为一些特殊效果准备了特定的工具,我们称之为“伪类”.其中有几项是我们经常用到的,下面我们就详细介绍一下经常用于定义链接样式的四个伪类,它们分别是: 1 :link 2 :visited 3 :h ...
- 不要使用SBJSON(json-framework)
不要使用SBJSON(json-framework) 文章目录 不知道为什么,在iOS开发中,有很多人使用 SBJSON (又被称作json-framework)来做JSON解析库.我想这是因为SBJ ...
- GridVeiw 使用
1. 因使用的是 Mongodb,因此要在 ActiveDataProvider 中指定 key 属性 2. 自定义表格中的按钮 'class' => 'yii\grid\ActionColum ...
- 连连看beta发布
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- swfit-pod使用
一.查询第三方版本号 pod search SDWebImage 二.项目添加pod 1.在终端打开项目路径 2.输入 pod init 生成Podfile 三.在Podfile输入需要的第三方 ...