我有个log文件 log.txt
想读取之后 在文件的最后一行添加一段String
比如
本来是:
2011-04-01 ok;
2011-04-02 ok;现在想实现
2011-04-01 ok;
2011-04-02 ok;
2011-04-03 ok;有什么好的实现方法
本来想过索性整个老的log读到BufferedOutputStream 然后加上新的String 重新写入
不过BOS或者String都有大小限制的吧
如果当log很大的话 就读不进buffer了吧?
想读取之后 在文件的最后一行添加一段String
比如
本来是:
2011-04-01 ok;
2011-04-02 ok;现在想实现
2011-04-01 ok;
2011-04-02 ok;
2011-04-03 ok;有什么好的实现方法
本来想过索性整个老的log读到BufferedOutputStream 然后加上新的String 重新写入
不过BOS或者String都有大小限制的吧
如果当log很大的话 就读不进buffer了吧?
FileOutputStream 有重载的构造器,如下
FileOutputStream(File file, boolean append)
等二个参数就是表明是否是添加到文件的未端的呀,默认为false,你把它显示为true直接写就可以了
append为false(默认值)时,输出流覆盖原文件内容,append为true时,将输出流追加到文件末尾。如果要另起一行的话要在输出流前加"\n"
import java.io.*;
public class IO_02 {
public static void main(String[] args) {
FileOutputStream out=null;
OutputStreamWriter wri=null;
try{
out=new FileOutputStream("wenjian/log.txt", true);
wri=new OutputStreamWriter(out);
wri.write("2011-04-03 ok;");
System.out.println("成功写入……");
wri.flush();
wri.close();
out.close();
}catch(IOException e){
System.out.println(e.getStackTrace());
}
finally{
try{
wri.close();
out.close();
}
catch(IOException e){
System.out.println(e.getStackTrace());
}
}
}
}