在Java中读取文本文件 [英] Reading a text file in java
问题描述
如何读取Java中的.txt文件,并在每行包含整数,字符串和双精度时将每行放入数组中?每行都有不同数量的单词/数字。
我是Java中的一个完整的noob,所以如果这个问题有点愚蠢的话。 //java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.htmlrel =noreferrer> 扫描仪
类没有人知道,但几乎可以做任何事情的文本。
要得到一个文件的读者,使用
档案档案=新档案(...路径...);
字符串编码=....; //编码你的文件
读者阅读器=新的BufferedReader(新的InputStreamReader(
新的FileInputStream(文件),编码));
...使用阅读器...
reader.close();
您应该指定编码,否则在遇到变音符号,Unicode和喜欢。
How would I read a .txt file in Java and put every line in an array when every lines contains integers, strings, and doubles? And every line has different amounts of words/numbers.
I'm a complete noob in Java so sorry if this question is a bit stupid.
Thanks
Try the Scanner
class which no one knows about but can do almost anything with text.
To get a reader for a file, use
File file = new File ("...path...");
String encoding = "...."; // Encoding of your file
Reader reader = new BufferedReader (new InputStreamReader (
new FileInputStream (file), encoding));
... use reader ...
reader.close ();
You should really specify the encoding or else you will get strange results when you encounter umlauts, Unicode and the like.
这篇关于在Java中读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!