简体   繁体   中英

Read Data From Txt File And Split It Into Paragraphs In Java

In case that I read data from txt file, how can I split it into paragraphs? Since a paragraph may contain more than one sentence, what delimiter I should use?

DATA FROM TXT:

Java is a computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another.

Java was originally developed by James Gosling at Sun Microsystems (which has since merged into Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them.

EXPECTED OUTPUT:

Paragraph 1 = Java is a computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another.

Paragraph 2 = Java was originally developed by James Gosling at Sun Microsystems (which has since merged into Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them.

I'm sorry for poor English. Thanks for your response.

When you detect end of paragraph you should add

System.getProperty("line.separator");

And at the begening of each paragraph add

("\t")

So simply it might looks using string builder.

StringBuilder builder = new StringBuilder();
builder.append("\t"); //begening
builder.append(PARAGRAPH_CONTENT);
builder.append(System.getProperty("line.separator"); //end of paragraph
System.out.println(builder.toString());

Your question is not much clear, but may be this is what you want :

try
    {
        BufferedReader bf = new BufferedReader(new FileReader("C:\\fileName.txt"));
        String line = bf.readLine();
        while (line != null)
        {
            System.out.println(line);
            line = bf.readLine();
        }

    }
    catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM