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接口实现 ...
随机推荐
- TYVJ P1036 统计数字 Label:坑!!!(用queue+map做出来的水)
背景 NOIP2007年提高组第一题 描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数 ...
- 【wikioi】1904 最小路径覆盖问题(最大流+坑人的题+最小路径覆盖)
http://wikioi.com/problem/1904/ 这题没看数据的话是一个大坑(我已报告官方修复了),答案只要求数量,不用打印路径...orz 最小路径覆盖=n-最大匹配,这个我在说二分图 ...
- TV测试中的按键长按操作模拟
从UiAutomator在TV测试中的局限性说起: 智能TV的操作和手机的操作有很大不同,一般智能TV的操作为遥控器按键操作,来向TV OS发送 KeyCode,以完成指定操作. UiAutomat ...
- Java/Js下使用正则表达式匹配嵌套Html标签
转自:http://www.jb51.net/article/24422.htm 以前写过一篇文章讲解如何使用正则表达式完美解决Html嵌套标签的匹配问题(使用正则表达式匹配嵌套Html标签),但是里 ...
- java实现吸血鬼数字
public class Vempire { public static void main(String[] arg) { String[] ar_str1, ar_str2; ; int from ...
- Wilddog - 野狗统计
根据业务需求提出的统计代码. <!DOCTYPE HTML> <html lang="en-US"> <head> <meta chars ...
- Eclipse中部署hadoop2.3.0
1 eclipse中hadoop环境部署概览 eclipse 中部署hadoop包括两大部分:hdfs环境部署和mapreduce任务执行环境部署.一般hdfs环境部署比较简单,部署后就 可以在ecl ...
- 将组策略中的内容导出至CSV格式
#将组策略首选项中的"本地用户和组"下的所有条目导出 $xml = Get-GPOReport -ReportType Xml -Name "China Desktop ...
- Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)
1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summe ...
- VIM常用快捷键~网页上查找
转自~木枫林 转自~鸟哥的私房菜 第十章.vim 程序编辑器 第十章.vim 程序编辑器 最近更新日期:2009/08/20 2. vi 的使用 2.1 简易执行范例 2.2 按键说明 2.3 一个案 ...