在增加文件大小后如何写入文本文件而没有连接线?-Java

时间:2014-10-18 06:38:08

标签: java filewriter bufferedwriter

我正在使用BufferedWriter将文本写入notepad.txt文件。程序为每个句子用新行写入文本,但是当我增加记事本的大小时,所有行都相互连接。如何阻止这种情况发生?

    bw.write(Text);   
    bw.close();  

因为在线路相互连接之后,程序不能再从文件中读取,因为线路结构不同而且无法识别它。

2 个答案:

答案 0 :(得分:1)

使用bw.newLine()插入换行符,以便它不会连接

答案 1 :(得分:0)

java中的PrintWriter类怎么样?

对于这种情况,这将是更好的选择之一。

该类的用法如下所示,

public void writeFile() {
    String someText = "some of my text";
    PrintWriter out = null;
    try {
        out = new PrintWriter(
                new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("myout.out")))));

        out.println(someText);
        out.flush();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally
    {

        if(out != null) out.close();
    }

}

过于回答,

此致