我已经在一个名为Sample.txt的简单的线替换未在Java中
This is line1
This is line2
这里一个文本文件,我想和更换新线下面的内容,我的意思是输出应该像
This is line1 and This is line2
和我的代码如下。
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\Users\\home\\Desktop\\Test\\Sample.txt"));
int i = 0;
while ((sCurrentLine = br.readLine()) != null) {
String sCurrentLine1 = sCurrentLine.replaceAll("\\n+", "0NL0");
System.out.println("Line No." + i + " " + sCurrentLine1);
i++;
}
当我打印,我得到的输出作为
Line No.0 This is line1
Line No.1 This is line2
请让我知道我可以取代这个新的生产线。
感谢
根据定义'的readLine()'读取_single_线和不包括结束换行符。 –
是你关于替换不工作的问题(因此你不能在你的结果中看到'0NL0'),或者你只是希望输出全部在1行?对于后者,请使用'print'而不是'println' –