java学习笔记-IO向文件末尾追加内容
2018-03-15 14:33
591 查看
最近被问到文件追加内容,今天百度学习了相关资料,现在记录一下:
三种方式:字节输出流,字符输出流,随机访问文件流public static void appendFile(){
//方法一
try {
FileOutputStream out = new FileOutputStream("G:/file.txt", true);
String content = "-- append the method one !---";
out.write(content.getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
//方法二
try {
FileWriter writer = new FileWriter("G:/file.txt", true);
String content = "-- append the method two !--";
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
//方法三
try {
RandomAccessFile accessFile = new RandomAccessFile("G:/file.txt", "rw");
//获取文件长度
long length = accessFile.length();
//设置文件指针移动到文件末尾
accessFile.seek(length);
String content = "-- append the method three !--";
accessFile.write(content.getBytes());
accessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
三种方式:字节输出流,字符输出流,随机访问文件流public static void appendFile(){
//方法一
try {
FileOutputStream out = new FileOutputStream("G:/file.txt", true);
String content = "-- append the method one !---";
out.write(content.getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
//方法二
try {
FileWriter writer = new FileWriter("G:/file.txt", true);
String content = "-- append the method two !--";
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
//方法三
try {
RandomAccessFile accessFile = new RandomAccessFile("G:/file.txt", "rw");
//获取文件长度
long length = accessFile.length();
//设置文件指针移动到文件末尾
accessFile.seek(length);
String content = "-- append the method three !--";
accessFile.write(content.getBytes());
accessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
相关文章推荐
- Java学习笔记——File类文件管理及IO读写、复制操作
- Java追加内容到文件末尾
- java追加内容到文件末尾
- java学习笔记:使用zip api进行文件解压缩以及不解压直接读取指定文件内容
- java追加内容到文件末尾
- java写入文件(文件内容末尾追加)
- java写入文件(文件内容末尾追加)
- java 追加内容到文件末尾的几种常用方法
- java写入文件(文件内容末尾追加)
- java_croe 学习笔记之新IO---java.nio 之内存映射文件
- java 追加内容到文件末尾的几种常用方法
- java 追加内容到文件末尾的几种常用方法
- Java文件IO学习笔记(二)---文件锁定
- [学习笔记]Java IO之文件
- java 追加内容到文件末尾的几种常用方法
- Java学习笔记——File类文件管理及IO读写、复制操作
- [学习笔记]Java文件IO操作------实现对文件的读取与写入
- [零散篇]Java学习笔记---Java的对象序列化以及文件IO处理
- java学习笔记 IO学习笔记1 文件
- java追加内容到文件末尾的几种常用方法