在Java中写入文本文件时插入换行符

问题描述:

我学习FileWriter时略有删除...最终目标是编写一个程序,它将生成一个.bat文件,该文件将由启动.jar的批处理代码执行。问题是,我不知道如何确保每个FileWriter.write();将打印在新的行...任何想法?

I have a slight delema with learning FileWriter... The ultimate goal is writing a program that will "spawn" a .bat file that will be executed by the batch code that launched the .jar. The problem is, I have no clue how to make sure that every FileWriter.write(); will print on a new line... Any ideas??

要创建新的生产线,只需追加一个换行符的是字符串的结尾:

To create new lines, simply append a newline character to the end of the string:

FileWriter writer = ...
writer.write("The line\n");



此外,的PrintWriter 类提供的方法,其自动附加换行符你(的编辑:它也会自动使用您的操作系统的正确换行字符串):

Also, the PrintWriter class provides methods which automatically append newline characters for you (edit: it will also automatically use the correct newline string for your OS):

PrintWriter writer = ...
writer.println("The line");