在Java中写入文本文件时插入换行符 [英] Inserting New Lines when Writing to a Text File in Java

查看:319
本文介绍了在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");

这篇关于在Java中写入文本文件时插入换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆