在JAVA中写文件和读文件的问题~~~请教~~~
我用FileOutputStream()和BufferedOutputStream()向文件里面写字符,
以下是我的代码,可以写入;
BufferedReader c=new BufferedReader(new InputStreamReader(System.in));
String inp;
boolean flag =true;
BufferedOutputStream myBuffer;
byte buff[]=new byte[128];
FileOutputStream myFile =new FileOutputStream(file);
myFile =new FileOutputStream("test.txt");
myBuffer =new BufferedOutputStream(myFile);
while (flag)
{
System.out.println("输入要建立的文件的内容: ");
inp =c.readLine();
if (inp.equalsIgnoreCase("exit"))
flag =false;
else
{
inp +="\n";
buff =inp.getBytes();
myBuffer.write(buff);
myBuffer.flush();
}
}
但是第二次写的数据会覆盖第一个写的数据,怎么在文件结果追加而不是覆盖呢
FileInputStream有个skip,但是FileOutputStream怎么跳过呢?
我想用File来打开文件,得到文件的长度,然后每次就在结果处写入数据用BufferedOutputStream的write(byte[] b, int offset, int length)
来写如数据,不过好象不对呀,不是出错就是还是会覆盖,该怎么做好呢?
要么是出错,要么就