在Java中似乎有不同的方法来读写文件数据。
我想从一个文件中读取ASCII数据。有哪些可能的方法和它们的区别?
我最喜欢的读取小文件的方法是使用一个BufferedReader和一个StringBuilder。它非常简单,而且一针见血(虽然不是特别有效,但对大多数情况来说已经很好了)。
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}
有人指出,在Java 7之后,你应该使用try-with-resources(即自动关闭)的功能。
try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
}
当我阅读这样的字符串时,我通常希望无论如何都要对每一行做一些字符串处理,所以就会选择这种实现。
如果我想把一个文件读成一个字符串,我总是使用Apache Commons IO 的IOUtils.toString()方法。你可以看看这里的源代码。
http://www.docjar.com/html/api/org/apache/commons/io/IOUtils.java.html
FileInputStream inputStream = new FileInputStream("foo.txt");
try {
String everything = IOUtils.toString(inputStream);
} finally {
inputStream.close();
}
而用Java 7就更简单了。
try(FileInputStream inputStream = new FileInputStream("foo.txt")) {
String everything = IOUtils.toString(inputStream);
// do something with everything string
}
ASCII是一个TEXT文件,所以你可以使用Readers
来读取。Java也支持使用InputStreams
从二进制文件读取。如果被读取的文件很大,那么你会想在FileReader
的基础上使用BufferedReader
来提高读取性能。
通过这篇文章了解如何使用 "读取器"。
我还建议你下载并阅读这本名为Thinking In Java的精彩(免费)书籍。
在Java 7中。
new String(Files.readAllBytes(...))
(docs) 或
Files.readAllLines(...)
在Java 8中。
Files.lines(..).forEach(...)
最简单的方法是使用Java中的Scanner
类和FileReader对象。简单的例子。
Scanner in = new Scanner(new FileReader("filename.txt"));
Scanner
有几种方法可以读入字符串、数字等...。你可以在Java文档页面上寻找更多这方面的信息。
例如,将整个内容读入一个String
。
StringBuilder sb = new StringBuilder();
while(in.hasNext()) {
sb.append(in.next());
}
in.close();
outString = sb.toString();
另外,如果你需要一个特定的编码,你可以用它来代替FileReader
。
new InputStreamReader(new FileInputStream(fileUtf8), StandardCharsets.UTF_8)
下面是另一种不用外部库的方法。
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public String readFile(String filename)
{
String content = null;
File file = new File(filename); // For example, foo.txt
FileReader reader = null;
try {
reader = new FileReader(file);
char[] chars = new char[(int) file.length()];
reader.read(chars);
content = new String(chars);
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader != null){
reader.close();
}
}
return content;
}
我不得不对不同的方式进行基准测试。 我将对我的发现进行评论,但简而言之,最快的方式是使用普通的BufferedInputStream而不是FileInputStream。 如果必须读取许多文件,那么三个线程将使总的执行时间减少到大约一半,但增加更多的线程将逐渐降低性能,直到使使用二十个线程完成的时间是只使用一个线程的三倍。
假设你必须读取一个文件,并对其内容做一些有意义的事情。
在这里的例子中,是从一个日志中读取行,并计算其中包含超过某个阈值的值。
所以我假设单行的Java 8Files.lines(Paths.get("/path/to/file.txt")).map(line -> line.split(";"))
不是一个选项。
我在Java 1.8、Windows 7以及SSD和HDD驱动器上进行了测试。
我写了六个不同的实现。
rawParse: 在FileInputStream上使用BufferedInputStream,然后一字节一字节地切行读取。 这优于任何其他单线程方法,但对于非ASCII文件来说可能非常不方便。
lineReaderParse。 在FileReader之上使用BufferedReader,逐行读取,通过调用String.split()来分割行。 这比rawParse慢了近20%。
lineReaderParseParallel: 这与lineReaderParse相同,但它使用了多个线程。 在所有情况下,这都是最快的选项。
nioFilesParse:使用java.nio.filesParse。 使用java.nio.files.Files.lines()
nioAsyncParse.使用一个带有完成处理程序和线程池的异步文件通道。 使用一个异步文件通道,带有一个完成处理程序和一个线程池。
nioMemoryMappedParse: 使用一个内存映射的文件. 这是一个很糟糕的想法,它的执行时间至少是其他实现的三倍。
这些是在四核i7和SSD驱动器上读取204个文件的平均时间,每个文件4 MB。 这些文件是在飞行中生成的,以避免磁盘缓存。
rawParse 11.10 sec
lineReaderParse 13.86 sec
lineReaderParseParallel 6.00 sec
nioFilesParse 13.52 sec
nioAsyncParse 16.06 sec
nioMemoryMappedParse 37.68 sec
我发现在SSD或HDD驱动器上运行的差异比我预期的要小,SSD大约快15%。 这可能是因为文件是在无碎片的HDD上生成的,而且它们是按顺序读取的,因此旋转的硬盘几乎可以像SSD一样执行。
我对nioAsyncParse实现的低性能感到惊讶。 要么是我以错误的方式实现了某些东西,要么是使用NIO和一个完成处理程序的多线程实现的性能与使用java.io API的单线程实现相同(甚至更差)。 而且使用完成处理程序的异步解析比直接在老流上实现的代码行数要长得多,而且正确实现起来也很棘手。
现在,这六个实现之后是一个包含它们的类,再加上一个可参数化的main()方法,可以玩转文件数量、文件大小和并发程度。 请注意,文件的大小会有正负20%的变化。 这是为了避免由于所有文件的大小完全相同而造成的任何影响。
rawParse
public void rawParse(final String targetDir, final int numberOfFiles) throws IOException, ParseException {
overrunCount = 0;
final int dl = (int) ';';
StringBuffer lineBuffer = new StringBuffer(1024);
for (int f=0; f<numberOfFiles; f++) {
File fl = new File(targetDir+filenamePreffix+String.valueOf(f)+".txt");
FileInputStream fin = new FileInputStream(fl);
BufferedInputStream bin = new BufferedInputStream(fin);
int character;
while((character=bin.read())!=-1) {
if (character==dl) {
// Here is where something is done with each line
doSomethingWithRawLine(lineBuffer.toString());
lineBuffer.setLength(0);
}
else {
lineBuffer.append((char) character);
}
}
bin.close();
fin.close();
}
}
public final void doSomethingWithRawLine(String line) throws ParseException {
// What to do for each line
int fieldNumber = 0;
final int len = line.length();
StringBuffer fieldBuffer = new StringBuffer(256);
for (int charPos=0; charPos<len; charPos++) {
char c = line.charAt(charPos);
if (c==DL0) {
String fieldValue = fieldBuffer.toString();
if (fieldValue.length()>0) {
switch (fieldNumber) {
case 0:
Date dt = fmt.parse(fieldValue);
fieldNumber++;
break;
case 1:
double d = Double.parseDouble(fieldValue);
fieldNumber++;
break;
case 2:
int t = Integer.parseInt(fieldValue);
fieldNumber++;
break;
case 3:
if (fieldValue.equals("overrun"))
overrunCount++;
break;
}
}
fieldBuffer.setLength(0);
}
else {
fieldBuffer.append(c);
}
}
}
lineReaderParse
public void lineReaderParse(final String targetDir, final int numberOfFiles) throws IOException, ParseException {
String line;
for (int f=0; f<numberOfFiles; f++) {
File fl = new File(targetDir+filenamePreffix+String.valueOf(f)+".txt");
FileReader frd = new FileReader(fl);
BufferedReader brd = new BufferedReader(frd);
while ((line=brd.readLine())!=null)
doSomethingWithLine(line);
brd.close();
frd.close();
}
}
public final void doSomethingWithLine(String line) throws ParseException {
// Example of what to do for each line
String[] fields = line.split(";");
Date dt = fmt.parse(fields[0]);
double d = Double.parseDouble(fields[1]);
int t = Integer.parseInt(fields[2]);
if (fields[3].equals("overrun"))
overrunCount++;
}
lineReaderParseParallel。
public void lineReaderParseParallel(final String targetDir, final int numberOfFiles, final int degreeOfParalelism) throws IOException, ParseException, InterruptedException {
Thread[] pool = new Thread[degreeOfParalelism];
int batchSize = numberOfFiles / degreeOfParalelism;
for (int b=0; b<degreeOfParalelism; b++) {
pool[b] = new LineReaderParseThread(targetDir, b*batchSize, b*batchSize+b*batchSize);
pool[b].start();
}
for (int b=0; b<degreeOfParalelism; b++)
pool[b].join();
}
class LineReaderParseThread extends Thread {
private String targetDir;
private int fileFrom;
private int fileTo;
private DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private int overrunCounter = 0;
public LineReaderParseThread(String targetDir, int fileFrom, int fileTo) {
this.targetDir = targetDir;
this.fileFrom = fileFrom;
this.fileTo = fileTo;
}
private void doSomethingWithTheLine(String line) throws ParseException {
String[] fields = line.split(DL);
Date dt = fmt.parse(fields[0]);
double d = Double.parseDouble(fields[1]);
int t = Integer.parseInt(fields[2]);
if (fields[3].equals("overrun"))
overrunCounter++;
}
@Override
public void run() {
String line;
for (int f=fileFrom; f<fileTo; f++) {
File fl = new File(targetDir+filenamePreffix+String.valueOf(f)+".txt");
try {
FileReader frd = new FileReader(fl);
BufferedReader brd = new BufferedReader(frd);
while ((line=brd.readLine())!=null) {
doSomethingWithTheLine(line);
}
brd.close();
frd.close();
} catch (IOException | ParseException ioe) { }
}
}
}
nioFilesParse
public void nioFilesParse(final String targetDir, final int numberOfFiles) throws IOException, ParseException {
for (int f=0; f<numberOfFiles; f++) {
Path ph = Paths.get(targetDir+filenamePreffix+String.valueOf(f)+".txt");
Consumer<String> action = new LineConsumer();
Stream<String> lines = Files.lines(ph);
lines.forEach(action);
lines.close();
}
}
class LineConsumer implements Consumer<String> {
@Override
public void accept(String line) {
// What to do for each line
String[] fields = line.split(DL);
if (fields.length>1) {
try {
Date dt = fmt.parse(fields[0]);
}
catch (ParseException e) {
}
double d = Double.parseDouble(fields[1]);
int t = Integer.parseInt(fields[2]);
if (fields[3].equals("overrun"))
overrunCount++;
}
}
}
nioAsyncParse。
public void nioAsyncParse(final String targetDir, final int numberOfFiles, final int numberOfThreads, final int bufferSize) throws IOException, ParseException, InterruptedException {
ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(numberOfThreads);
ConcurrentLinkedQueue<ByteBuffer> byteBuffers = new ConcurrentLinkedQueue<ByteBuffer>();
for (int b=0; b<numberOfThreads; b++)
byteBuffers.add(ByteBuffer.allocate(bufferSize));
for (int f=0; f<numberOfFiles; f++) {
consumerThreads.acquire();
String fileName = targetDir+filenamePreffix+String.valueOf(f)+".txt";
AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get(fileName), EnumSet.of(StandardOpenOption.READ), pool);
BufferConsumer consumer = new BufferConsumer(byteBuffers, fileName, bufferSize);
channel.read(consumer.buffer(), 0l, channel, consumer);
}
consumerThreads.acquire(numberOfThreads);
}
class BufferConsumer implements CompletionHandler<Integer, AsynchronousFileChannel> {
private ConcurrentLinkedQueue<ByteBuffer> buffers;
private ByteBuffer bytes;
private String file;
private StringBuffer chars;
private int limit;
private long position;
private DateFormat frmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public BufferConsumer(ConcurrentLinkedQueue<ByteBuffer> byteBuffers, String fileName, int bufferSize) {
buffers = byteBuffers;
bytes = buffers.poll();
if (bytes==null)
bytes = ByteBuffer.allocate(bufferSize);
file = fileName;
chars = new StringBuffer(bufferSize);
frmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
limit = bufferSize;
position = 0l;
}
public ByteBuffer buffer() {
return bytes;
}
@Override
public synchronized void completed(Integer result, AsynchronousFileChannel channel) {
if (result!=-1) {
bytes.flip();
final int len = bytes.limit();
int i = 0;
try {
for (i = 0; i < len; i++) {
byte by = bytes.get();
if (by=='\n') {
// ***
// The code used to process the line goes here
chars.setLength(0);
}
else {
chars.append((char) by);
}
}
}
catch (Exception x) {
System.out.println(
"Caught exception " + x.getClass().getName() + " " + x.getMessage() +
" i=" + String.valueOf(i) + ", limit=" + String.valueOf(len) +
", position="+String.valueOf(position));
}
if (len==limit) {
bytes.clear();
position += len;
channel.read(bytes, position, channel, this);
}
else {
try {
channel.close();
}
catch (IOException e) {
}
consumerThreads.release();
bytes.clear();
buffers.add(bytes);
}
}
else {
try {
channel.close();
}
catch (IOException e) {
}
consumerThreads.release();
bytes.clear();
buffers.add(bytes);
}
}
@Override
public void failed(Throwable e, AsynchronousFileChannel channel) {
}
};
所有案例的全面运行执行情况。
[https://github.com/sergiomt/javaiobenchmark/blob/master/FileReadBenchmark.java][1]
[1]: https://github.com/sergiomt/javaiobenchmark/blob/master/FileReadBenchmark.java
以下是三种工作和测试方法。
BufferedReader
。package io;
import java.io.*;
public class ReadFromFile2 {
public static void main(String[] args)throws Exception {
File file = new File("C:\\Users\\pankaj\\Desktop\\test.java");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while((st=br.readLine()) != null){
System.out.println(st);
}
}
}
package io;
import java.io.File;
import java.util.Scanner;
public class ReadFromFileUsingScanner {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\pankaj\\Desktop\\test.java");
Scanner sc = new Scanner(file);
while(sc.hasNextLine()){
System.out.println(sc.nextLine());
}
}
}
FileReader
。package io;
import java.io.*;
public class ReadingFromFile {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("C:\\Users\\pankaj\\Desktop\\test.java");
int i;
while ((i=fr.read()) != -1){
System.out.print((char) i);
}
}
}
Scanner
类无循环地读取整个文件。package io;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadingEntireFileWithoutLoop {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("C:\\Users\\pankaj\\Desktop\\test.java");
Scanner sc = new Scanner(file);
sc.useDelimiter("\\Z");
System.out.println(sc.next());
}
}
我记录了[15种在Java中读取文件的方法][1],然后测试了它们在不同文件大小下的速度--从1 KB到1 GB,以下是三大方法。
java.nio.file.Files.readAllBytes()
。经测试可在Java 7、8和9中工作。
导入java.io.File.IOException;。 import java.io.IOException; import java.nio.file.Files.Files;
公共类ReadFile_Files_ReadAllBytes {。 public static void main(String [] pArgs) throws IOException {。 String fileName = "c:\temp\sample-10KB.txt"。 File file = new File(fileName);
byte [] fileBytes = Files.readAllBytes(file.toPath()); file.toPath() for(byte b : fileBytes) { = file.toPath(); char singleChar; for(byte b : fileBytes) singleChar = (char) b; System.out.print(singleChar); } } }
java.io.BufferedReader.readLine()
。经测试可在Java 7、8、9中工作。
import java.io.BufferedReader; import java.io.FileReader; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileReader; import java.io.FileReader; import java.io.FileReader; import java.io.FileReader; import java.io.FileReader; import java.io.FileReader; import java.io.FileReader; import java.io.FileReader; import java.io.FileReader。 import java.io.FileReader; import java.io.IOException;
public class ReadFile_BufferedReader_ReadLine { public static void main(String [] args) throws IOException {。 String fileName = "c:\temp\sample-10KB.txt"。 FileReader fileReader = new FileReader(fileName)。
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) { 字符串line.readLine()) != null while((line = bufferedReader.readLine()) != null) { System.out.println(line); } } } }
java.nio.file.Files.lines()
。这个测试在Java 8和9中工作,但在Java 7中不能工作,因为有lambda表达式的要求。
import java.io.File.IOException; import java.io.IOException; import java.IO.IOException; import java.IO.IOException; import java.IO.IOException; import java.IO.IOException; import java.IO.IOException; import java.IO.IOException。 import java.io.IOException; import java.nio.file.Files.Files; import java.util.stream.Stream;
public class ReadFile_Files_Lines { public static void main(String[]pArgs) throws IOException { String fileName = "c:\temp\sample-10KB.txt"。 File file = new File(fileName)。
try (Stream linesStream = Files.lines(file.toPath())) { linesStream.forEach(line -> { System.out.println(line); }); } } }
这基本上和Jesus Ramos'.的回答完全一样,只是用File代替了FileReader,再加上迭代来浏览文件内容。 答案完全一样,只是用File代替了FileReader,再加上迭代来遍历文件的内容。
Scanner in = new Scanner(new File("filename.txt"));
while (in.hasNext()) { // Iterates each line in the file
String line = in.nextLine();
// Do something with line
}
in.close(); // Don't forget to close resource leaks
... 抛出 "FileNotFoundException"。
使用BufferedReader。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
BufferedReader br;
try {
br = new BufferedReader(new FileReader("/fileToRead.txt"));
try {
String x;
while ( (x = br.readLine()) != null ) {
// Printing out each line in the file
System.out.println(x);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
catch (FileNotFoundException e) {
System.out.println(e);
e.printStackTrace();
}
缓冲流类在实践中的性能更强,以至于NIO.2 API包含了专门返回这些流类的方法,部分原因是为了鼓励你在应用中始终使用缓冲流。
下面是一个例子。
Path path = Paths.get("/myfolder/myfile.ext");
try (BufferedReader reader = Files.newBufferedReader(path)) {
// Read from the stream
String currentLine = null;
while ((currentLine = reader.readLine()) != null)
//do your code here
} catch (IOException e) {
// Handle file I/O exception...
}
你可以替换这段代码
BufferedReader reader = Files.newBufferedReader(path);
与
BufferedReader br = new BufferedReader(new FileReader("/myfolder/myfile.ext"));
笔者推荐本篇文章,学习Java NIO和IO的主要用途。
在Java中从文件中读取数据最简单的方法就是利用File类来读取文件,利用Scanner类来读取文件的内容。
public static void main(String args[])throws Exception
{
File f = new File("input.txt");
takeInputIn2DArray(f);
}
public static void takeInputIn2DArray(File f) throws Exception
{
Scanner s = new Scanner(f);
int a[][] = new int[20][20];
for(int i=0; i<20; i++)
{
for(int j=0; j<20; j++)
{
a[i][j] = s.nextInt();
}
}
}
PS.不要忘记导入java.util.;。 不要忘记导入java.util.。 才能让Scanner工作。
你可以使用readAllLines和join
方法在一行中获取整个文件内容。
String str = String.join("\n",Files.readAllLines(Paths.get("e:\\text.txt")));
它默认使用UTF-8编码,可以正确读取ASCII数据。
此外,您也可以使用readAllBytes。
String str = new String(Files.readAllBytes(Paths.get("e:\\text.txt")), StandardCharsets.UTF_8);
我认为readAllBytes更快、更精确,因为它不会用n`代替新行,而且新行也可能是
r/n`。
这取决于你的需求,哪一个是合适的。
这可能不是问题的确切答案。 它只是另一种读取文件的方式,在这种方式下,你不在Java代码中明确指定文件的路径,而是将其作为命令行参数来读取。
用下面的代码。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class InputReader{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s="";
while((s=br.readLine())!=null){
System.out.println(s);
}
}
}
只是去运行它与。
java InputReader < input.txt
这将读取input.txt
的内容,并将其打印到你的控制台。
你也可以让你的System.out.println()
通过命令行写到一个特定的文件,如下所示。
java InputReader < input.txt > output.txt
这将从 "input.txt "读取并写入 "output.txt"。
番石榴][1]为此提供了一个单行本。
import com.google.common.base.Charsets;
import com.google.common.io.Files;
String contents = Files.toString(filePath, Charsets.UTF_8);