Java:将文件读入数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/285712/
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
Java: Reading a file into an array
提问by Northener
I have a file (called "number.txt") which I want to read to an array in Java. How exactly do I go ahead and do this? It is a straight-forward "1-dimensional" file, containing 100 numbers.
我有一个文件(称为“number.txt”),我想将它读入 Java 中的数组。我该如何继续并做到这一点?它是一个直接的“一维”文件,包含 100 个数字。
The problem is that I get an exception every time. Apparently it can't find it (I am sure its spelled correctly). When looking through code examples, it doesn't specify the file's entire file path, only the name of the file itself. How would I go about doing that if its necessary?
问题是我每次都会遇到异常。显然它找不到它(我确定它的拼写正确)。在查看代码示例时,它没有指定文件的整个文件路径,只指定文件本身的名称。如果有必要,我将如何去做?
Also, when reading the file, will the array automatically contain all the lines of the file, or will I have to make a loop which which copies every line to corresponding subscript i?
另外,在读取文件时,数组是否会自动包含文件的所有行,还是必须创建一个循环将每一行复制到相应的下标 i?
I've heard of BufferedReader class, what it's purpose, and how does it corelate to reading input?
我听说过 BufferedReader 类,它的用途是什么,以及它如何与读取输入相关联?
回答by toolkit
Here is some example code to help you get started:
以下是一些示例代码,可帮助您入门:
package com.acme;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FileArrayProvider {
public String[] readLines(String filename) throws IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
}
}
And an example unit test:
以及一个示例单元测试:
package com.acme;
import java.io.IOException;
import org.junit.Test;
public class FileArrayProviderTest {
@Test
public void testFileArrayProvider() throws IOException {
FileArrayProvider fap = new FileArrayProvider();
String[] lines = fap
.readLines("src/main/java/com/acme/FileArrayProvider.java");
for (String line : lines) {
System.out.println(line);
}
}
}
Hope this helps.
希望这可以帮助。
回答by Ryan Thames
You should be able to use forward slashes in Java to refer to file locations.
您应该能够在 Java 中使用正斜杠来引用文件位置。
The BufferedReader class is used for wrapping other file readers whos read method may not be very efficient. A more detailed description can be found in the Java APIs.
BufferedReader 类用于包装其他读取方法可能效率不高的文件读取器。可以在Java API 中找到更详细的描述。
Toolkit's use of BufferedReader is probably what you need.
Toolkit 对 BufferedReader 的使用可能正是您所需要的。
回答by Tony Nassar
Apache Commons I/O provides FileUtils#readLines(), which should be fine for all but huge files: http://commons.apache.org/io/api-release/index.html. The 2.1 distribution includes FileUtils.lineIterator(), which would be suitable for large files. Google's Guava libraries include similar utilities.
Apache Commons I/O 提供 FileUtils#readLines(),除了大文件外,它应该适用于所有文件:http: //commons.apache.org/io/api-release/index.html。2.1 发行版包括 FileUtils.lineIterator(),它适用于大文件。Google 的 Guava 库包括类似的实用程序。
回答by Hélio Santos
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
// ...
Path filePath = new File("fileName").toPath();
Charset charset = Charset.defaultCharset();
List<String> stringList = Files.readAllLines(filePath, charset);
String[] stringArray = stringList.toArray(new String[]{});