java如何追加写入txt文件
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文件的更多相关文章
- java追加写入txt文件
整理了下网上的资料,数据追加写入txt文件有三种方式,见下面代码: 方法一: public void method1() { FileWriter fw = null; try { //如果文件存在, ...
- Java——读取和写入txt文件
package com.java.test.a; import java.io.BufferedReader; import java.io.BufferedWriter; import java.i ...
- java读取数据写入txt文件并将读取txt文件写入另外一个表
package com.xsw.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.F ...
- java读取和写入txt文件
package com.yinghuo.testDES; import java.io.BufferedReader;import java.io.BufferedWriter;import java ...
- JAVA读取TXT文件、新建TXT文件、写入TXT文件
1.创建TXT文件 按照正常的逻辑写就好 先定义一个文件给定一个路径——>判断这个路径上这个文件存不存在——>若不存在则建立,try/catch根据程序提示自动生成就好 2.读取TXT文件 ...
- Java读取txt文件和写入txt文件
package com.nickwong.code; import java.io.*; /** * Created by Nickwong on 31/07/2018. * 根据1-8楼的建议,优化 ...
- Java读取CSV数据并写入txt文件
读取CSV数据并写入txt文件 package com.vfsd; import java.io.BufferedWriter; import java.io.File; import java.io ...
- Java代码输出到txt文件(申请专利贴源码的必备利器)
最近公司在申请专利,编写不少文档,项目的代码量实在是过于庞大.如果一个一个的复制粘贴虽然能够完成,但是对于程序员而言实在没有这个必要.shell或者python就能解决这个问题.由于我个人对于shel ...
- 生成大量插入语句,并将语句写入txt文件中
import java.io.*; /** * Created by czz on 2019/9/23. */ public class TTest { /** * 生成大量插入语句,并将语句写入tx ...
随机推荐
- golang的goroutine与channel
Golang的goroutine是非抢占式的, 令人相当蛋疼! 有痛不能呻吟...只能配合channel在各goroutine之间传递信号来实现抢占式, 而这形成了golang最灵活与最具性能的核心. ...
- AngularJS 授权 + Node.js REST api
作者好屌啊,我不懂的他全都懂. Authentication with AngularJS and a Node.js REST api 几个月前,我开始觉得 AngularJS 好像好牛逼的样子,于 ...
- UIToolbar swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- nodejs tools
1.supervisor npm install supervisor -g supervisor app.js cd public cd bin supervisor www http://www. ...
- android 开发 解码gif图片,获取每帧bitmap
环境:android 4.3 (注意对于android4.4版本解码出来不正确,除了第一帧正确外,其余的都是显示不同的地方) 通用版本见: android 开发对gif解码(适配android 4 ...
- How to Build FFmpeg for Android
http://www.roman10.net/how-to-build-ffmpeg-for-android/ ffmpeg is an open-source platform for record ...
- 图解SQL的Join(转摘)
转摘网址:http://coolshell.cn/articles/3463.html 对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的 ...
- HDU 2196 求树上所有点能到达的最远距离
其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了... 但是乱搞一下还是有80分....可惜没想到正解啊! 所以今天的考试题是: 巡访 (path.pas/c/cpp) Cha ...
- Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- ActiveMQ主从配置
这种方式有个问题,activemq1有消息没消费完但是突然宕机,虽然程序会自动连到activemq2.但是activemq1的消息只有等机器恢复后才会被消费. 1.启动:我这里使用的是apache-a ...