Split text file into Strings on empty line

后端 未结 6 1425
温柔的废话
温柔的废话 2021-02-15 16:12

I want to read a local txt file and read the text in this file. After that i want to split this whole text into Strings like in the example below .

Example : Lets say

6条回答
  •  臣服心动
    2021-02-15 16:52

    The below code would work even if there are more than 2 empty lines between useful data.

    import java.util.regex.*;
    
    // read your file and store it in a string named str_file_data
    
    Pattern p = Pattern.compile("\\n[\\n]+");     /*if your text file has \r\n as the newline character then use Pattern p = Pattern.compile("\\r\\n[\\r\\n]+");*/
    String[] result = p.split(str_file_data);
    

    (I did not test the code so there could be typos.)

提交回复
热议问题