Split text file into Strings on empty line

后端 未结 6 1453
温柔的废话
温柔的废话 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:28

    you can split a string to an array by

    String.split();
    

    if you want it by new lines it will be

    String.split("\\n\\n");
    

    UPDATE*

    If I understand what you are saying then john.

    then your code will essentially be

    BufferedReader in
       = new BufferedReader(new FileReader("foo.txt"));
    
    List allStrings = new ArrayList();
    String str ="";
    while(true)
    {
        String tmp = in.readLine();
        if(tmp.isEmpty())
        {
          if(!str.isEmpty())
          {
              allStrings.add(str);
          }
          str= "";
        }
        else if(tmp==null)
        {
            break;
        }
        else
        {
           if(str.isEmpty())
           {
               str = tmp;
           }
           else
           { 
               str += "\\n" + tmp;
           }
        }
    }
    

    Might be what you are trying to parse.

    Where allStrings is a list of all of your strings.

提交回复
热议问题