Java输入输出(I/O)操作指南
在Java编程中,输入输出(I/O)操作是不可或缺的一部分。无论是读取文件、写入数据,还是处理网络通信,I/O操作都扮演着关键角色。本文将带你深入了解Java I/O操作的各个方面,助你轻松掌握这一重要技能。

一、Java I/O概述
Java的I/O操作主要分为两大类:字节流和字符流。字节流用于处理二进制数据,而字符流则用于处理文本数据。Java提供了丰富的类库来支持这些操作,主要集中在java.io包中。
1.1 字节流
字节流以字节为单位进行数据传输,适用于所有类型的文件。常见的字节流类包括:
InputStream:抽象类,表示输入字节流。OutputStream:抽象类,表示输出字节流。FileInputStream:用于从文件中读取数据。FileOutputStream:用于向文件中写入数据。
1.2 字符流
字符流以字符为单位进行数据传输,特别适用于文本文件。常见的字符流类包括:
Reader:抽象类,表示输入字符流。Writer:抽象类,表示输出字符流。FileReader:用于从文件中读取字符。FileWriter:用于向文件中写入字符。
二、文件I/O操作
文件I/O操作是Java I/O中最常见的应用场景。以下是一个简单的示例,展示如何使用字节流读取和写入文件。
2.1 读取文件
import java.io.FileInputStream;
import java.io.IOException;
public class FileReadExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("input.txt")) {
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.2 写入文件
import java.io.FileOutputStream;
import java.io.IOException;
public class FileWriteExample {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream("output.txt")) {
String text = "Hello, World!";
fos.write(text.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、缓冲流的使用
为了提高I/O操作的效率,Java提供了缓冲流。缓冲流在内存中创建一个缓冲区,减少实际I/O操作的次数。
3.1 缓冲字节流
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferedReadExample {
public static void main(String[] args) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("input.txt"))) {
int data;
while ((data = bis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.2 缓冲字符流
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
四、高级I/O操作
除了基本的文件I/O,Java还支持更高级的I/O操作,如对象序列化、随机访问文件等。
4.1 对象序列化
对象序列化允许将对象转换为字节流,以便存储或传输。
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student implements Serializable {
private static final long serialVersionUID = 1L;
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class SerializationExample {
public static void main(String[] args) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.ser"))) {
Student student = new Student("Alice", 20);
oos.writeObject(student);
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.2 随机访问文件
RandomAccessFile类支持随机访问文件,既可以读取也可以写入。
import java.io.RandomAccessFile;
import java.io.IOException;
public class RandomAccessExample {
public static void main(String[] args) {
try (RandomAccessFile r
« 上一篇
Java网络编程与Socket应用探秘
下一篇 »
Java异常处理机制与最佳实践