我在名为 Sample.txt
的文本文件中提供了以下内容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
请让我知道如何更换这条新线。
由于
答案 0 :(得分:1)
您不需要执行BufferedReader::readLine()
\n
方法会从sCurrentLine
中返回的字符串中删除try {
String sCurrentLine;
StringBuilder sb = new StringBuilder();// Declare a string builder object.
br = new BufferedReader(new FileReader("C:\\Users\\home\\Desktop\\Test\\Sample.txt"));
int i = 0;
while ((sCurrentLine = br.readLine()) != null) {
if(i>0) {
sb.append(" and ");
}
sb.append(sCurrentLine);
System.out.println("Line No." + i + " " + sCurrentLine);
i++;
}
System.out.println("Appended output " + sb.toString());
个字符。所以你要做的就是追加返回的行。
示例:
swal({
html: true,
title: "Message",
text: 'Hello World',
type: "success",
}, function () {
// steps if success.
});
答案 1 :(得分:-1)
试试这个,
Created Date: 1/1/2016
First Name Last Name Middle Name Address1 Address2 City
Sam Test M 123 test Drive 1 England
William Adam A 123 Circle Apt 2013 New York
No of records for 1/1/2016 : 2
Created Date: 1/3/2016
First Name Last Name Middle Name Address1 Address2 City
Aaron Silva B 546 Wood Dr Plaza Delhi
Kapil Sam R 750 Parkwoo Circle Los Angles
Asha Tucker C 1234 Main Dr Briar Rd Dallas
No of records for 1/1/2016 : 3
希望它对你有所帮助。