java中,对文件进行追加内容操作的三种方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
//如果文件存在,则追加内容;如果文件不存在,则创建文件,追加内容的三种方法
public class AppendContentToFile {
@SuppressWarnings("static-access")
public static void main(String[] args) {
AppendContentToFile a = new AppendContentToFile();
a.method1();
a.method2("E:\\dd.txt""222222222222222");
a.method3("E:\\dd.txt""33333333333");
}

方法1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void method1() {
FileWriter fw = null;
try {
//如果文件存在,则追加内容;如果文件不存在,则创建文件
File f=new File("E:\\dd.txt");
fw = new FileWriter(f, true);
catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(fw);
pw.println("追加内容");
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
catch (IOException e) {
e.printStackTrace();
}
}

方法2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void method2(String file, String conent) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true)));
out.write(conent+"\r\n");
catch (Exception e) {
e.printStackTrace();
finally {
try {
out.close();
catch (IOException e) {
e.printStackTrace();
}
}
}

方法3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void method3(String fileName, String content) {
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
// 将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content+"\r\n");
randomFile.close();
catch (IOException e) {
e.printStackTrace();
}
}
}

java如何追加写入txt文件的更多相关文章

  1. java追加写入txt文件

    整理了下网上的资料,数据追加写入txt文件有三种方式,见下面代码: 方法一: public void method1() { FileWriter fw = null; try { //如果文件存在, ...

  2. Java——读取和写入txt文件

    package com.java.test.a; import java.io.BufferedReader; import java.io.BufferedWriter; import java.i ...

  3. java读取数据写入txt文件并将读取txt文件写入另外一个表

    package com.xsw.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.F ...

  4. java读取和写入txt文件

    package com.yinghuo.testDES; import java.io.BufferedReader;import java.io.BufferedWriter;import java ...

  5. JAVA读取TXT文件、新建TXT文件、写入TXT文件

    1.创建TXT文件 按照正常的逻辑写就好 先定义一个文件给定一个路径——>判断这个路径上这个文件存不存在——>若不存在则建立,try/catch根据程序提示自动生成就好 2.读取TXT文件 ...

  6. Java读取txt文件和写入txt文件

    package com.nickwong.code; import java.io.*; /** * Created by Nickwong on 31/07/2018. * 根据1-8楼的建议,优化 ...

  7. Java读取CSV数据并写入txt文件

    读取CSV数据并写入txt文件 package com.vfsd; import java.io.BufferedWriter; import java.io.File; import java.io ...

  8. Java代码输出到txt文件(申请专利贴源码的必备利器)

    最近公司在申请专利,编写不少文档,项目的代码量实在是过于庞大.如果一个一个的复制粘贴虽然能够完成,但是对于程序员而言实在没有这个必要.shell或者python就能解决这个问题.由于我个人对于shel ...

  9. 生成大量插入语句,并将语句写入txt文件中

    import java.io.*; /** * Created by czz on 2019/9/23. */ public class TTest { /** * 生成大量插入语句,并将语句写入tx ...

随机推荐

  1. Java之XStream之下划线问题

    XStream将Java对象序列为XML, 如果对象属性带有下划线,输出到XML时会转成"__"双下划. 网上很多资料说是Bug, XStream开发的大神没有哪么笨吧? 查了一下 ...

  2. df du

    df命令详细用法 a:显示全部的档案系统和各分割区的磁盘使用情形 i:显示i -nodes的使用量 k:大小用k来表示 (默认值) t:显示某一个档案系统的所有分割区磁盘使用量 x:显示不是某一个档案 ...

  3. Html5最简单的游戏Demo——Canvas绘图的弹弹球

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  4. iOS 进阶 第十八天(0423)

    0423 - GCD( Grand Central Dispatch) block复习 请问,图中输出结果是多少?为什么? 答:结果是10.因为在定义block的时候,block会把它前面的要用到的变 ...

  5. c++中的struct

    c++中的struct不在是c中的struct,不仅仅是一个多个数据类型的结构体了.c++中的struct可以具有成员函数(c语言中是不可以的),c++ struct还可以继承class等等.同时c+ ...

  6. 单元测试篇----cppUnit的安装与使用

    在刚学习单元测试章节的时候,尝试着使用dev—c++来编译cppunit,但一直没成功,也尝试问过同学,一直没有很好的方法,因此浪费了不少时间.今天又耐心的尝式一下,意外成功了.以下是详细的安装步骤: ...

  7. iTween

    http://u3d.as/content/pixelplacement/i-tween/1s9 download http://itween.pixelplacement.com/documenta ...

  8. 二、freemarker.controller半自动静态化+Tomcat虚拟资源映射

    描述:本内容主要是讲2个tomcat之间同时共享一个静态话页面,统一入口是springMVC的一个controller,静态化的更新只需要传false.true.把完成的web项目放入a.b服务器To ...

  9. Node.js 项目搭建

    关于 本书致力于教会你如何用Node.js来开发应用,过程中会传授你所有所需的“高级”JavaScript知识.本书绝不是一本“Hello World”的教程. 状态 你正在阅读的已经是本书的最终版. ...

  10. Hibernate中启用日志

    Problem How do you determine what SQL query is being executed by Hibernate? How can you see the Hibe ...