Java 字节流与字符流
字节流和字符流是 Java IO 的核心。理解它们的使用是进行文件读写的基础。本章将详细介绍 Java 中的字节流和字符流。
输入/输出字节流
InputStream(字节输入流)
InputStream 是所有字节输入流的抽象基类。
核心方法:
// 读取一个字节
int read() throws IOException;
// 读取到字节数组
int read(byte[] b) throws IOException;
int read(byte[] b, int off, int len) throws IOException;
// 跳过字节
long skip(long n) throws IOException;
// 关闭流
void close() throws IOException;
// 标记和重置(如果支持)
void mark(int readlimit);
void reset();
boolean markSupported();
常用子类:
FileInputStream:文件输入流ByteArrayInputStream:字节数组输入流BufferedInputStream:缓冲输入流DataInputStream:数据输入流
OutputStream(字节输出流)
OutputStream 是所有字节输出流的抽象基类。
核心方法:
// 写入一个字节
void write(int b) throws IOException;
// 写入字节数组
void write(byte[] b) throws IOException;
void write(byte[] b, int off, int len) throws IOException;
// 刷新缓冲区
void flush() throws IOException;
// 关闭流
void close() throws IOException;
常用子类:
FileOutputStream:文件输出流ByteArrayOutputStream:字节数组输出流BufferedOutputStream:缓冲输出流DataOutputStream:数据输出流
输入/输出字符流
Reader(字符输入流)
Reader 是所有字符输入流的抽象基类。
核心方法:
// 读取一个字符
int read() throws IOException;
// 读取到字符数组
int read(char[] cbuf) throws IOException;
int read(char[] cbuf, int off, int len) throws IOException;
// 跳过字符
long skip(long n) throws IOException;
// 关闭流
void close() throws IOException;
常用子类:
FileReader:文件字符输入流CharArrayReader:字符数组输入流BufferedReader:缓冲字符输入流InputStreamReader:字节流转字符流
Writer(字符输出流)
Writer 是所有字符输出流的抽象基类。
核心方法:
// 写入一个字符
void write(int c) throws IOException;
// 写入字符数组
void write(char[] cbuf) throws IOException;
void write(char[] cbuf, int off, int len) throws IOException;
// 写入字符串
void write(String str) throws IOException;
void write(String str, int off, int len) throws IOException;
// 追加
Writer append(CharSequence csq) throws IOException;
// 刷新缓冲区
void flush() throws IOException;
// 关闭流
void close() throws IOException;
常用子类:
FileWriter:文件字符输出流CharArrayWriter:字符数组输出流BufferedWriter:缓冲字符输出流OutputStreamWriter:字符流转字节流
常用子类
FileInputStream
FileInputStream 用于从文件读取字节。
import java.io.FileInputStream;
import java.io.IOException;
// 读取文件
try (FileInputStream fis = new FileInputStream("file.txt")) {
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
}
// 使用缓冲区读取(推荐)
try (FileInputStream fis = new FileInputStream("file.txt")) {
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
System.out.write(buffer, 0, length);
}
}
FileOutputStream
FileOutputStream 用于向文件写入字节。
import java.io.FileOutputStream;
import java.io.IOException;
// 写入文件
try (FileOutputStream fos = new FileOutputStream("file.txt")) {
fos.write(65); // 写入字节 'A'
fos.write("Hello".getBytes());
}
// 追加模式
try (FileOutputStream fos = new FileOutputStream("file.txt", true)) {
fos.write(" World".getBytes());
}
FileReader
FileReader 用于从文件读取字符。
import java.io.FileReader;
import java.io.IOException;
// 读取文件
try (FileReader fr = new FileReader("file.txt")) {
int data;
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
}
// 使用缓冲区读取(推荐)
try (FileReader fr = new FileReader("file.txt")) {
char[] buffer = new char[1024];
int length;
while ((length = fr.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, length));
}
}
FileWriter
FileWriter 用于向文件写入字符。
import java.io.FileWriter;
import java.io.IOException;
// 写入文件
try (FileWriter fw = new FileWriter("file.txt")) {
fw.write("Hello");
fw.write(" World");
fw.flush(); // 刷新缓冲区
}
// 追加模式
try (FileWriter fw = new FileWriter("file.txt", true)) {
fw.write(" Appended");
}
示例:文本文件读写
示例 1:使用字节流读写文本
import java.io.*;
public class ByteStreamExample {
// 写入文本文件
public static void writeTextFile(String filename, String content) throws IOException {
try (FileOutputStream fos = new FileOutputStream(filename)) {
fos.write(content.getBytes("UTF-8"));
}
}
// 读取文本文件
public static String readTextFile(String filename) throws IOException {
try (FileInputStream fis = new FileInputStream(filename)) {
byte[] buffer = new byte[1024];
StringBuilder sb = new StringBuilder();
int length;
while ((length = fis.read(buffer)) != -1) {
sb.append(new String(buffer, 0, length, "UTF-8"));
}
return sb.toString();
}
}
public static void main(String[] args) throws IOException {
writeTextFile("test.txt", "Hello, 世界!");
String content = readTextFile("test.txt");
System.out.println(content);
}
}