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 ...
随机推荐
- Linux下cron的使用
cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务: /sbin/service c ...
- angularjs resources
http://tutorials.jenkov.com/angularjs/watch-digest-apply.html http://angular-tips.com/blog/2013/08/w ...
- c语言参数类型
今天看ntcip源码时看到,函数参数有点不一样.在函数实现时,没有括号中没有指明参数类型.注意这里说的是函数实现,不是说函数声明.这里在函数列表括号后面做了类型的说明,以前看到过,没想起来,今天做个记 ...
- 将centos系统中的网卡em1还原为eth0
1.修改系统grub 操作:vi /boot/grub/grub.conf 增加一个 biosdevname=0 的启动参数 示例: [root@xingfujie ~]# cat /boot/gru ...
- IT服务系统组成
软件+硬件+数据 + 运维人员 = IT服务系统 车 司机 乘客 修车 = 车模式 效率 系统 用户 业务 运维 = 信息化 效率 如果司机不会开车,没有人会修车就不会有车轮上的世界 同样没有人会运维 ...
- My97DatePicker控制开始时间和结束时间区间
开始时间: <input type="text" placeholder=" -请选择- " id="kssj" name=" ...
- jquery动态加载JS【方法getScript】的改进
http://www.cnblogs.com/cuitsl/archive/2012/11/15/2771549.html
- Struts2中的ModelDriven机制及其运用(转)
所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象.比如,有实体类User如下: package cn.com.leadfar.struts2.actions; public cla ...
- PP生产订单的BADI增强 WORKORDER_UPDATE
METHOD if_ex_workorder_update~before_update. *---------------------->增强1 开始* "当生产订单类型为PP01时, ...
- android编程常见问题- Resource ID #0x7f070001 type #0x12 is not valid
问题说明: AndroidRuntime(1705): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.e ...