写入文件时插入换行符? [英] Insert line break when writing to file?
本文介绍了写入文件时插入换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
try {
while((line = br.readLine() )!= null){
Matcher m = urlPattern.matcher(line); (m.find()){
System.out.println(m.group(1));
while
// println在每次查找后放入换行符
String filename =page.txt;
FileWriter fw = new FileWriter(filename,true);
fw.write(m.group(1));
fw.close();
// fw在每次查找之后写入所有内容而不换行
}
}
我在 System.out.println(m.group(1));
处得到正确的输出形式。希望写出 m.group(1)
所显示的内容,因为代码中没有换行符,所以它不会写入换行符。
只要调用
fw.write(System.getProperty(line.separator));
。 / p> System.getProperty(line.separator)
会为您提供您的平台的行分隔符(无论是Windows还是一些Unix风格)。
So My code looks like this:
try {
while ((line = br.readLine()) != null) {
Matcher m = urlPattern.matcher (line);
while (m.find()) {
System.out.println(m.group(1));
//the println puts linebreak after each find
String filename= "page.txt";
FileWriter fw = new FileWriter(filename,true);
fw.write(m.group(1));
fw.close();
//the fw writes everything after each find with no line break
}
}
I get right form of output at line System.out.println(m.group(1));
However when I later on want to write what is shown by m.group(1)
It writes to file without putting linebreak since the code doesn't have one.
解决方案
Just call fw.write(System.getProperty("line.separator"));
.
System.getProperty("line.separator")
will give you the line separator for your platform (whether Windows or some Unix flavor).
这篇关于写入文件时插入换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文