英文:
After reading with BufferReader '\n' won't be accepted as a new line char, how to solve this?
问题
这是我使用BufferedReader
和BufferedWriter
的代码:
public static void readAndWrite(String fileNameToRead, String fileNameToWrite) {
try {
BufferedReader fr = new BufferedReader(
new FileReader(String.format("%s.txt", fileNameToRead)));
BufferedWriter out = new BufferedWriter(
new FileWriter(String.format("%s.txt", fileNameToWrite), true));
String currentTmp = "";
String tmp = "";
String test = "work \nwork"; // 这是测试字符串
out.append(test);
while ((tmp = fr.readLine()) != null) {
tmp = tmp.trim();
if (tmp.isEmpty()) {
currentTmp = currentTmp.trim();
out.append(currentTmp);
out.newLine();
out.newLine();
currentTmp = "";
} else {
currentTmp = currentTmp.concat(" ").concat(tmp);
}
}
if (!currentTmp.equals("")) {
out.write(currentTmp);
}
fr.close();
out.close();
} catch (IOException e) {
System.out.println("exception occoured" + e);
}
}
public static void main(String[] args) {
String readFile = "inPutFile";
String writeFile = "outPutFile";
readAndWrite(readFile, writeFile);
}
问题在于代码中的test
字符串包含换行符\n
,在使用BufferedWriter
输出时可以正确换行,但是如果将相同的字符串放入文本文件中,效果就不一样了。
更容易看出的问题是,我希望我的输入文件内容如下:
work\n
work
而输出应为:
work
work
由于我使用的是 macOS,所以换行符应为\n
。
英文:
I have a large text file I want to format. Say the input file is called inputFile
and output file is called outputFile
.
This is my code for using BufferedReader
and BufferedWriter
Here is my code
public static void readAndWrite(String fileNameToRead, String fileNameToWrite) {
try{
BufferedReader fr = new BufferedReader(
new FileReader(String.format("%s.txt", fileNameToRead)));
BufferedWriter out = new BufferedWriter(
new FileWriter(String.format("%s.txt", fileNameToWrite), true));
String currentTmp = "";
String tmp = "";
String test = "work \nwork";
out.append(test);
while((tmp = fr.readLine()) != null) {
tmp = tmp.trim();
if(tmp.isEmpty()) {
currentTmp = currentTmp.trim();
out.append(currentTmp);
out.newLine();
out.newLine();
currentTmp = "";
} else {
currentTmp = currentTmp.concat(" ").concat(tmp);
}
}
if(!currentTmp.equals("")) {
out.write(currentTmp);
}
fr.close();
out.close();
} catch (IOException e) {
System.out.println("exception occoured" + e);
}
}
public static void main(String[] args) {
String readFile = "inPutFile";
String writeFile = "outPutFile";
readAndWrite(readFile, writeFile);
}
The problem is that the test
string inside the code which have '\n' can we converted to a new line with BufferedWriter
. But if I put the same string in the text file it would not perform the same.
In a more easy way to see is that I want my input file have this
work\n
work
and output as
work
work
I am using mac, so the separator should be '\n'
答案1
得分: 0
工作
如果您在文件中看到"\n",那不是换行符号。它只是两个字符。
trim()
方法不会移除这些字符。
相反,您可能会有类似这样的代码:
if (tmp.endsWith("\n")
tmp = tmp.substring(0, tmp.length() - 2);
> 我使用的是 mac,所以分隔符应该是 '\n'
您应该使用适用于平台的换行符。因此,在向文件写入代码时应该是:
} else {
currentTmp = currentTmp.concat(" ").concat(tmp);
out.append( currentTmp );
out.newLine();
}
newLine()
方法将使用适用于平台的适当换行字符串。
编辑:
您需要理解 Java 中的转义字符。当您使用:
String text = "test\n"
并将该字符串写入文件时,文件只会写入 5 个字符,而不是 6 个。"\n"
是一个转义序列,它会导致新行字符的 ASCII 值被添加到文件中。这个字符无法显示,所以您在文件中看不到它。
英文:
work\n
if you see the "\n" in your file, it is not a new line character. It is just two characters.
The trim()
method will not remove those characters.
Instead you might have something like:
if (tmp.endsWith("\n")
tmp = tmp.substring(0, tmp.length() - 2);
> I am using mac, so the separator should be '\n'
You should use the newline character for the platform. So when writing to your file the code should be:
} else {
currentTmp = currentTmp.concat(" ").concat(tmp);
out.append( currentTmp );
out.newLine();
}
The newline() method will use the appropriate new line String for the platform.
Edit:
You need to understand what an escape character is in Java. When you use:
String text = "test\n"
and write the string to a file, only 5 characters are written to the file, not 6. The "\n" is an escape sequence which will cause the ascii value for the new line character to be added to the file. This character is not displayable so you can't see it in the file.
答案2
得分: -1
在 @camickr 的回答之后,我想我意识到了问题。不知何故,如果文件中有这样的文本:
work \nwork
其中的 \n
不会被视为单个字符('\n'),而是被视为两个字符。我想这就是为什么当 BufferWriter
写入输入字符串时,它不会将其视为新行。
英文:
After @camickr answer, I think I realized the problem. Some how if I have a text in the file like this
work \nwork
The \n
won't be treated as a single char ('\n'), rather it has been treated as two chars. I think thats why when the BufferWriter
writes the input string it won't treat it as a new line.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论