Java – stores the contents of a text file line by line into an array

All, the problem I'm facing now is that I don't know how to store the contents of the text file in the array

Is it possible for me to store them row by row in an array? What I expect is this:

I've tried something like this, but it doesn't seem to work for me If someone can help me, I really appreciate it

The code is:

Solution

I suggest using an ArrayList to handle dynamic size, and an array will need a defined size, which you may not know You can reset the list to an array at any time@ H_ 502_ 3@BufferedReader in = new BufferedReader(new FileReader("path/of/text")); String str; List
list = new ArrayList

(); while((str = in.readLine()) != null){ list.add(str); } String[] stringArr = list. toArray(new String[0]);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>