BufferedReader: read multiple lines into a single string

后端 未结 7 1215
逝去的感伤
逝去的感伤 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:40

    You have a file containing doubles. Looks like you have more than one number per line, and may have multiple lines.

    Simplest thing to do is read lines in a while loop.

    You could return null from your ReadBigStringIn method when last line is reached and terminate your loop there.

    But more normal would be to create and use the reader in one method. Perhaps you could change to a method which reads the file and returns an array or list of doubles.

    BTW, could you simply split your strings by whitespace?

    Reading a whole file into a single String may suit your particular case, but be aware that it could cause a memory explosion if your file was very large. Streaming approach is generally safer for such i/o.

提交回复
热议问题