java 从文件中读取数据的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7072612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to read data from a file
提问by user793384
Possible Duplicate:
Best way to read a text file
可能的重复:
阅读文本文件的最佳方式
In Java I can open a text file like this:
在 Java 中,我可以打开这样的文本文件:
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
My question is, how do you read from the following file? The first line is a number (830) representing number of words, and the following lines contain the words.
我的问题是,你如何从以下文件中读取?第一行是一个数字(830),代表单词的数量,接下来的几行包含单词。
830
cooking
English
weather
.
.
I want to read the words into a string array. But how do I read the data first?
我想将单词读入字符串数组。但是我如何先读取数据?
回答by maerics
You're on the right track; I would treat the first line as a special case by parsing it as an integer (see Integer#parseInt(String)
) then reading the words as individual lines:
你在正确的轨道上;我会将第一行视为一个特殊情况,将其解析为整数(请参阅Integer#parseInt(String)
),然后将单词作为单独的行读取:
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
String numLinesStr = reader.readLine();
if (numLinesStr == null) throw new Exception("invalid file format");
List<String> lines = new ArrayList<String>();
int numLines = Integer.parseInt(numLinesStr);
for (int i=0; i<numLines; i++) {
lines.add(reader.readLine());
}
回答by Dragon8
If you're working with Java version greater than 1.5, you can also use the Scanner class:
如果您使用的 Java 版本高于 1.5,您还可以使用 Scanner 类:
Scanner sc = new Scanner(new File("someTextFile.txt"));
List<String> words = new ArrayList<String>();
int lines = sc.nextInt();
for(int i = 1; i <= lines; i++) {
words.add(sc.nextLine());
}
String[] w = words.toArray(new String[]{});
回答by Snps
Unless you have some special reason, it's not necessary to keep track of how many lines the file contain. Just use something like this:
除非有特殊原因,否则没有必要跟踪文件包含多少行。只需使用这样的东西:
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
String line;
while ((line = reader.readLine()) != null) {
// ...
}
回答by Jim
Try the class java.io.BufferedReader, created on a java.io.FileReader. This object has the method readLine, which will read the next line from the file:
试试在 java.io.FileReader 上创建的 java.io.BufferedReader 类。该对象具有 readLine 方法,它将从文件中读取下一行:
try
{
java.io.BufferedReader in =
new java.io.BufferedReader(new java.io.FileReader("filename.txt"));
String str;
while((str = in.readLine()) != null)
{
...
}
}
catch(java.io.IOException ex)
{
}
回答by Shawn
You could use reflection and do this dynamically:
您可以使用反射并动态执行此操作:
public static void read() {
try {
BufferedReader reader = new BufferedReader(new FileReader(
"filename.txt"));
String line = reader.readLine();
while (line != null) {
if (Integer.class.isAssignableFrom(line.getClass())) {
int number = Integer.parseInt(line);
System.out.println(number);
} else {
String word = line;
System.out.println(word);
}
line = reader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}