BufferedReader: read multiple lines into a single string

后端 未结 7 1110
逝去的感伤
逝去的感伤 2021-01-17 11:22

I\'m reading numbers from a txt file using BufferedReader for analysis. The way I\'m going about this now is- reading a line using .readline, splitting this string into an a

7条回答
  •  野的像风
    2021-01-17 11:45

    If you create a StringBuilder, then you can append every line to it, and return the String using toString() at the end.

    You can replace your ReadBigStringIn() with

    public String ReadBigStringIn() {
            StringBuilder b = new StringBuilder();
    
            try {
                String line = buffIn.readLine();
                while (line != null) {
                    b.append(line);
                    line = buffIn.readLine();
                }
            }
            catch(IOException e){};
    
            return b.toString();
    }
    

提交回复
热议问题