PackageCn.edu.shu.web.test;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileReader;ImportJava.io.IOException;ImportJava.util.ArrayList;ImportJava.util.Iterator;/** * * <p> * ClassName getparagraph * </p> * <p> * Description use Java to complete the segmentation of a natural paragraph of text, where five text formats are given as example, in either format, the program can be precisely divided. * </p> * * @author tkpad [email protected] * <p> * Date February 11, 2015 1:33:03 * </p> * @version V1.0.0 * */ Public class getparagraph { Public Static void Main(string[] args)throwsIOException {arraylist<string> res =NewArraylist<string> ();//Segmentation resultsStringBuilder SB =NewStringBuilder ();//splicing Read contentString temp =NULL;//Temp variable, store SB to remove the contents of the space //BufferedReader reader = new BufferedReader (new FileReader (New File ("C:\\users\\tkpad\\desktop\\a.txt" )); //BufferedReader reader= new BufferedReader (New FileReader (New File ("C:\\users\\tkpad\\desktop\\b.txt" )); //BufferedReader reader= new BufferedReader (New FileReader (New File ("C:\\users\\tkpad\\desktop\\c.txt" )); //BufferedReader reader = new BufferedReader (new FileReader (New File ("C:\\users\\tkpad\\desktop\\d.txt" ));BufferedReader reader =NewBufferedReader (NewFileReader (NewFile ("C:\\users\\tkpad\\desktop\\e.txt")));intCH =0; while(ch = reader.read ())! =-1) {temp = sb.tostring (). Trim (). ReplaceAll ("\\s*","");//Remove the space before and after removing the middle space if((Char) ch = =' \ r ') {//Determine if it is a blank line if(!"". Equals (temp)) {//Description to the end of the paragraph, add it to the list, and empty SBRes.add (temp); } sb.delete (0, Sb.length ()); }Else{//Description not at the end of paragraph, the result is stagedSb.append ((Char) ch); } }if(Reader.read () = =-1) {System.out.println ("Haha, you read to the end!" "); }//Last paragraph if not null, the last paragraph will be added, otherwise not processed if(!"". Equals (temp)) {Res.add (temp); } iterator<string> Iterator = Res.iterator (); while(Iterator.hasnext ()) {String next = Iterator.next (); System.out.println ("Paragraph start:"); System.out.println (next); } System.out.println ("The number of paragraphs is:"+ res.size ()); }}
The test text has been packaged and uploaded, please download it.
Address:
http://download.csdn.net/detail/shijiebei2009/8440133
Java implementation text is sliced according to natural paragraphs