【问题标题】:How do write into a text file, without the lines connecting after you increase the file size?-Java增加文件大小后如何写入文本文件而不连接行?-Java
【发布时间】:2014-10-18 06:38:08
【问题描述】:

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

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

因为线连接后,程序无法再从文件中读取,因为线结构不同,无法识别。

【问题讨论】:

  • "程序用换行符写入文本 [...]" 你的小代码 sn-p 准确地表明你写的文本没有换行符。通过在您的BufferedWriter 上调用newLine(),您将得到您想要的,正如@MrunalGosar 的回答所解释的那样。

标签: java filewriter bufferedwriter


【解决方案1】:

使用 bw.newLine() 插入换行符,这样就不会连接了

【讨论】:

  • 它已经插入了一个新行,但是当我增加记事本寡妇大小时,所有行开始连接以填充空间。
  • @danny 尝试在写字板中打开
  • 如果你能发布你得到的输出和你期望的输出将会很有帮助。
  • 在 notepad++ 中打开该文档并打开自动换行。
  • 按照本答案作者的建议,致电bw.newLine()。这会为新行插入您的平台字符序列,在 Windows 上为 "\r\n"。您很可能只写了一个"\n" 作为字符串的一部分,当您在记事本中打开它时,您会看到一行,因为没有在文件中写入 windows 新行序列。
【解决方案2】:

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();
    }

}

回答太迟了,

问候,

【讨论】:

    猜你喜欢
    • 2014-02-25
    • 1970-01-01
    • 2013-12-02
    • 2014-06-20
    • 2012-04-15
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多