Java的读文件和存储字符串到一个数组 [英] Java reading a file and storing string into an array
问题描述
可能重复:结果
存储文件内容到一个数组
块引用>我编写一个简单的刽子手程序。我有打开一个文件,然后将数据存储到一个数组中的困难。我似乎无法弄清楚如何做到这一点。这是一个学校项目,所以如果逻辑上似乎是不必要的,只怪男人:)
我在这十个字的文本文件(words.txt)。它们与一个行的文件,在每个人之间。他们需要被导入到一个数组。任何人都关心,帮助一个年轻有抱负的程序员呢?感谢您的帮助,您可以给!
解决方案我建议你使用
列表<弦乐>
(如的ArrayList<串GT;
)和扫描
(构造它使用的新的扫描仪(新文件(wordfile.txt中))
,并使用scanner.hasNextLine()
/的scanner.nextLine()
)来读的话。如果您确实需要它是一个数组,通过的
list.toArray
到底。 p>Possible Duplicate:
Storing file content into an arrayI'm programming a simple hangman program. I am having difficulty with opening a file, and then storing the data into an array. I can't seem to figure out how to do it. This is for a school project so if the logic seems unnecessary, just blame the man :)
I have a text file (words.txt) with ten words in it. They are in the file with a line in between each of them. They need to be imported into an array. Anyone care to help a young aspiring programmer out? Thanks for any help you can give!
解决方案I suggest you use a
List<String>
(such asArrayList<String>
) and aScanner
(construct it usingnew Scanner(new File("wordfile.txt"))
, and usescanner.hasNextLine()
/scanner.nextLine()
) to read the words.If you indeed need it to be an array, go through
list.toArray
in the end.这篇关于Java的读文件和存储字符串到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!