Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要从文件中单独提取一个段落并使其成为一行_Java_Regex_String - Fatal编程技术网

Java 需要从文件中单独提取一个段落并使其成为一行

Java 需要从文件中单独提取一个段落并使其成为一行,java,regex,string,Java,Regex,String,我有一个文本文件作为输入,其中我有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

我有一个文本文件作为输入,其中我有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。什么也救不了我(

如果有任何内容,我需要用空格替换新行

请帮忙


首先,我为我的英语感到抱歉。 我想你可以读每一行,然后把每一行串成一个字符串

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();
            }
        }
    }

我希望它能帮助你

我认为最好是解析这样的输入:
Stackoverflow是java开发人员讨论非常有趣和复杂问题的网站。它不是为了向初学者询问“我如何编写我的程序”。
展示你的代码并解释它是如何工作的。str=str.replaceAll(“,”);str=str.replaceAll(“,”);str=str.replace(“\n”,”);