Java的读文件和存储字符串到一个数组 [英] Java reading a file and storing string into an array

查看:190
本文介绍了Java的读文件和存储字符串到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  存储文件内容到一个数组


我编写一个简单的刽子手程序。我有打开一个文件,然后将数据存储到一个数组中的困难。我似乎无法弄清楚如何做到这一点。这是一个学校项目,所以如果逻辑上似乎是不必要的,只怪男人:)

我在这十个字的文本文件(words.txt)。它们与一个行的文件,在每个人之间。他们需要被导入到一​​个数组。任何人都关心,帮助一个年轻有抱负的程序员呢?感谢您的帮助,您可以给!


解决方案

我建议你使用列表<弦乐> (如 的ArrayList<串GT; )和扫描(构造它使用的 新的扫描仪(新文件(wordfile.txt中)) ,并使用 scanner.hasNextLine() /的 scanner.nextLine() )来读的话。

如果您确实需要它是一个数组,通过的 list.toArray 到底。

Possible Duplicate:
Storing file content into an array

I'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 as ArrayList<String>) and a Scanner (construct it using new Scanner(new File("wordfile.txt")), and use scanner.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆