我有一个文本文件作为输入,我有n个段落,每个段落都有换行符。 我需要将每个段落替换为一行
让我考虑一个小例子
我的意见是:
Stackoverflow is the best site for
java novice beginners
it helps them to identify the solutions for unsolved problems
我的输出应该是:
Stackoverflow is the best site for java novice beginners it helps them to identify the solutions for unsolved problems Please help me to fix it.
我尝试使用split,regex。什么都不能救我。 :(
如果有任何内容,我需要用空格替换新行
请帮忙。
由于
答案 0 :(得分:0)
首先抱歉我的英语。 我想你可以读取每一行,然后在一个字符串中连接每一行。像这样:
File yourFile= null;
FileReader fr = null;
BufferedReader br = null;
//Here you will store the merged paragraphs
String mergedParagraphs = new String();
try
{
yourFile= new File ("path to your file");
fr = new FileReader (yourfile);
br = new BufferedReader(fr);
String line;
while((line=br.readLine())!=null)
{
mergedParagraphs = mergedParagraphs + line;
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
// Here the input file is closed
try
{
if( null != fr )
{
fr.close();
}
}catch (Exception e2)
{
e2.printStackTrace();
}
}
}
我希望它可以帮到你