java如何对文件追加写入【new FileOutputStream(file, true)】
2015-09-24 09:09
801 查看
java文件追加内容的三种方法:
方法一:
public static void writeToTxtByRandomAccessFile(File file, String str){
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file,"rw");
long len = randomAccessFile.length();
randomAccessFile.seek(len);
randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法二:
public static void writeToTxtByFileWriter(File file, String content){
BufferedWriter bw = null;
try {
FileWriter fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法三:
public static void writeToTxtByOutputStream(File file, String content){
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));
bufferedOutputStream.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e ){
e.printStackTrace();
}finally{
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法一:
public static void writeToTxtByRandomAccessFile(File file, String str){
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file,"rw");
long len = randomAccessFile.length();
randomAccessFile.seek(len);
randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法二:
public static void writeToTxtByFileWriter(File file, String content){
BufferedWriter bw = null;
try {
FileWriter fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法三:
public static void writeToTxtByOutputStream(File file, String content){
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));
bufferedOutputStream.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e ){
e.printStackTrace();
}finally{
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- NGUI 3.5过程(三)Button button
- UITableView出现多余的cell
- HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)
- Double类parseDouble和valueOf方法的区别
- [Hash思想]UVa1152 - 4 Values whose Sum is 0
- [高效算法 deque运用]UVa120 - Stacks of Flapjacks
- 图Uyui
- Arduino(一)
- iOS图片浏览器控件 放大,缩小,UIScrollView
- Xcode, does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE) 解决办法
- iOS9 UI Tests探索笔记
- 【UIKit-124-10】#import <UIKit/UIView.h>
- uibutton
- iOS-Core-Animation-Advanced-Techniques(七-3)
- Android shrinkResources true 引发的血案
- iOS-Core-Animation-Advanced-Techniques(七-2)
- iOS-Core-Animation-Advanced-Techniques(七-1)
- POJ 2778 DNA Sequence (AC自动机+矩阵快速幂)
- iOS-Core-Animation-Advanced-Techniques(四-5)
- ubuntu 14.04安装quickbuild buildagent (二)