如何在Java中读取文件
在这篇文章中,你将学习如何使用Java提供的各种类和实用方法,如BufferedReader
、LineNumberReader
、Files.readAllLines
、Files.lines
、BufferedInputStream
、Files.readAllBytes
等,在Java中读取文本文件或二进制(图像)文件。
让我们借助例子来看看在Java中读取文件的每一种不同方法。
Java使用缓冲读取器读取文件
BufferedReader是一种在Java中读取文本文件的简单而有效的方法。它从一个字符输入流中读取文本。它对字符进行缓冲,以提供高效的阅读。
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class BufferedReaderExample {
public static void main(String[] args) {
Path filePath = Paths.get("demo.txt");
Charset charset = StandardCharsets.UTF_8;
try (BufferedReader bufferedReader = Files.newBufferedReader(filePath, charset)) {
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}
Java使用Files.readAllLines()逐行读取文件
Files.readAllLines()是Java NIO的Files类的一个实用方法,它读取一个文件的所有行并返回一个包含每一行的List<String>
。它内部使用BufferedReader来读取文件。
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class FilesReadAllLinesExample {
public static void main(String[] args) {
Path filePath = Paths.get("demo.txt");
Charset charset = StandardCharsets.UTF_8;
try {
List<String> lines = Files.readAllLines(filePath, charset);
for(String line: lines) {
System.out.println(line);
}
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}
Java使用Files.lines()逐行读取文件
Files.lines()方法从一个文件中读取所有的行,作为一个Stream
。你可以使用Stream API方法,如forEach
,map
来处理文件的每一行。
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FilesLinesExample {
public static void main(String[] args) {
Path filePath = Paths.get("demo.txt");
Charset charset = StandardCharsets.UTF_8;
try {
Files.lines(filePath, charset)
.forEach(System.out::println);
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}
Java使用LineNumberReader逐行读取文件
LineNumberReader是一个缓冲的字符流阅读器,它可以跟踪行号。你可以使用这个类来逐行读取一个文本文件。
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class LineNumberReaderExample {
public static void main(String[] args) {
Path filePath = Paths.get("demo.txt");
Charset charset = StandardCharsets.UTF_8;
try(BufferedReader bufferedReader = Files.newBufferedReader(filePath, charset);
LineNumberReader lineNumberReader = new LineNumberReader(bufferedReader)) {
String line;
while ((line = lineNumberReader.readLine()) != null) {
System.out.format("Line %d: %s%n", lineNumberReader.getLineNumber(), line);
}
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}
Java使用BufferedInputStream读取二进制文件(图像文件)。
到目前为止,本文介绍的所有例子都是从一个字符输入流中读取文本数据的。如果你要读一个二进制数据,如图像文件,那么你需要使用一个字节输入流。
缓冲输入流(BufferedInputStream)让你读取原始的字节流。它还对输入进行缓冲以提高性能。
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
public class BufferedInputStreamImageCopyExample {
public static void main(String[] args) {
try(InputStream inputStream = Files.newInputStream(Paths.get("sample.jpg"));
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
OutputStream outputStream = Files.newOutputStream(Paths.get("sample-copy.jpg"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
byte[] buffer = new byte[4096];
int numBytes;
while ((numBytes = bufferedInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, numBytes);
}
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}
Java使用Files.readAllBytes()将文件读成[]字节。
如果你想在一个字节数组中读取整个文件的内容,那么你可以使用Files.readAllBytes()方法。
import com.sun.org.apache.xpath.internal.operations.String;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FilesReadAllBytesExample {
public static void main(String[] args) {
try {
byte[] data = Files.readAllBytes(Paths.get("demo.txt"));
// Use byte data
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}