从文本文件和 returns 一个 json 字符串获取输入的程序
Program which takes input from a text file and returns a json string
这是我的文本文件的示例
JUNE 15
Wednesday
BH 259: Trusting Jesus
TRUSTING GOD
Text: Hebrews 11:7-12
RBT: 1 Chron. 14-15;
John 18:19-40
By faith Abraham, when called to go to a place he would later
receive as his inheritance, obeyed and went, even though he
did not know where he was going. Hebrews 11:8 (NIV)
输出会是这样的
JUNE 15\nWednesday\nBH 259: Trusting Jesus\nTRUSTING GOD\nText: Hebrews 11:7-12\n\nRBT: 1 Chron. 14-15;\nJohn 18:19-40\n
我尝试通过 fileinputstream 上的 read() 读取文件
in = new FileInputStream("2_jun.txt");
out = new FileOutputStream("output.txt");
int c;
while((c = in.read()) != -1){
out.write(c);
}
但我无法比较行尾
因为我必须用 "\n"
替换 EOL
关于如何实现这一点有什么建议吗?
如果您使用 java-8,您可能需要使用 Files#lines
。
它会 return 一个 Stream<String>
,每个条目都是读取文件的一行。
然后您可以随意处理它们。
String result;
try(Stream<String> lines = Files.lines(Paths.get("2_june.txt")){
result = lines.map(x -> x + "\n")
.collect(Collectors.joining());
}
这是我的文本文件的示例
JUNE 15
Wednesday
BH 259: Trusting Jesus
TRUSTING GOD
Text: Hebrews 11:7-12
RBT: 1 Chron. 14-15;
John 18:19-40
By faith Abraham, when called to go to a place he would later
receive as his inheritance, obeyed and went, even though he
did not know where he was going. Hebrews 11:8 (NIV)
输出会是这样的
JUNE 15\nWednesday\nBH 259: Trusting Jesus\nTRUSTING GOD\nText: Hebrews 11:7-12\n\nRBT: 1 Chron. 14-15;\nJohn 18:19-40\n
我尝试通过 fileinputstream 上的 read() 读取文件
in = new FileInputStream("2_jun.txt");
out = new FileOutputStream("output.txt");
int c;
while((c = in.read()) != -1){
out.write(c);
}
但我无法比较行尾 因为我必须用 "\n"
替换 EOL关于如何实现这一点有什么建议吗?
如果您使用 java-8,您可能需要使用 Files#lines
。
它会 return 一个 Stream<String>
,每个条目都是读取文件的一行。
然后您可以随意处理它们。
String result;
try(Stream<String> lines = Files.lines(Paths.get("2_june.txt")){
result = lines.map(x -> x + "\n")
.collect(Collectors.joining());
}