日期:2014-05-20  浏览次数:20729 次

在写文件的时候怎么将输入可以换行?
请各为大虾们指点       本人的这段代码不可以换行输入,请问在IO中用什么函数可以在写的时候换行。     还有,用哪个方法可以不将写好的文件里的内容给从新覆盖public   void   setFile()   throws   IOException{
        System.out.println( "Please   write   words: ");
        try   {
FileOutputStream   os   =   new   FileOutputStream( "D:/OutputTest.ini ");
int   n   =   1024,   count;
byte   bt[]   =   new   byte[n];
count   =   System.in.read(bt);
os.write(bt,0,count);
os.close();
System.out.println( "Save   is   ok! ");

                }   catch   (FileNotFoundException   e)   {
e.printStackTrace();
}
}
这种方法会把以前输入了的内容覆盖,我不想让它覆盖以前的内容而继续写,也希望能够换行输入,请各位指点。

------解决方案--------------------
用 RandomAccessFile来实现,在网上帮你搜了个小例子
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileTest {
public RandomAccessFileTest() {
try {
RandomAccessFile dos = new RandomAccessFile( "tmp1 ", "rw ");
dos.writeInt(1);
dos.seek(0);
System.out.println(dos.readInt());
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String xx[]) {
new RandomAccessFileTest();
}
}
------解决方案--------------------
FileOutputStream有一个带boolean变量的构造函数
public FileOutputStream(File file,
boolean append)
throws FileNotFoundException
append - if true, then bytes will be written to the end of the file rather than the beginning

------解决方案--------------------
没上面那么复杂 你直接在你要换行的地方 append( "\r\n ") 就ok 了 我做过这个东西的.呵呵 你试试就知道了