Java IO 基础知识总结

在 Java 编程中,IO(Input/Output,输入 / 输出)操作是至关重要的一部分,它使得程序能够与外部系统进行交互,比如读取文件、写入数据到数据库或者在网络上传输信息等。今天,我们就来深入了解一下 Java IO 的基础知识。

一、流(Stream)的概念

流是 Java IO 的核心抽象,它代表着数据的流动。可以把流想象成一个管道,数据从一端流入,从另一端流出。Java 中有两种基本的流:输入流(InputStream)和输出流(OutputStream),分别用于读取和写入数据。输入流从数据源(如文件、网络连接等)读取数据到程序中,而输出流则将程序中的数据写入到目的地(同样可以是文件、网络等)。

二、字节流与字符流

  1. 字节流
    • 字节流以字节(8 位)为单位处理数据,是最基础的流类型。InputStream 是所有字节输入流的抽象父类,常见的实现类有 FileInputStream,用于从文件中读取字节数据。例如:
复制代码

try (FileInputStream fis = new FileInputStream("test.txt")) {

int data;

while ((data = fis.read())!= -1) {

System.out.print((char) data);

}

} catch (IOException e) {

e.printStackTrace();

}

  • OutputStream 是字节输出流的抽象父类,FileOutputStream 用于向文件写入字节数据:
复制代码

try (FileOutputStream fos = new FileOutputStream("output.txt")) {

String text = "Hello, World!";

byte[] bytes = text.getBytes();

fos.write(bytes);

} catch (IOException e) {

e.printStackTrace();

}

  1. 字符流
    • 字符流以字符(16 位,对于 Unicode 字符)为单位处理文本数据,更适合处理文本文件,因为它能正确处理字符编码。Reader 是字符输入流的抽象父类,FileReader 可读取字符文件:
复制代码

try (FileReader fr = new FileReader("test.txt")) {

int c;

while ((c = fr.read())!= -1) {

System.out.print((char) c);

}

} catch (IOException e) {

e.printStackTrace();

}

  • Writer 是字符输出流的抽象父类,FileWriter 用于写入字符数据:
复制代码

try (FileWriter fw = new FileWriter("output.txt")) {

fw.write("This is a text.");

} catch (IOException e) {

e.printStackTrace();

}

三、缓冲流(Buffered Stream)

缓冲流是为了提高 IO 性能而设计的。它在内部维护了一个缓冲区,减少了直接对底层数据源或目的地的频繁读写操作。BufferedInputStream 和 BufferedOutputStream 是字节缓冲流,BufferedReader 和 BufferedWriter 是字符缓冲流。例如,使用 BufferedReader 读取文件:

复制代码

try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {

String line;

while ((line = br.readLine())!= -1) {

System.out.println(line);

}

} catch (IOException e) {

e.printStackTrace();

}

四、序列化与反序列化

序列化是将对象转换为字节序列以便存储或传输的过程,反序列化则是相反的操作,将字节序列还原为对象。在 Java 中,要使一个对象可序列化,需要实现 Serializable 接口。例如:

复制代码

import java.io.Serializable;

class Person implements Serializable {

private String name;

private int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

}

// getters and setters

}

public class SerializationExample {

public static void main(String[] args) {

Person person = new Person("John", 30);

try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.ser"))) {

oos.writeObject(person);

} catch (IOException e) {

e.printStackTrace();

}

try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.ser"))) {

Person deserializedPerson = (Person) ois.readObject();

System.out.println(deserializedPerson.getName() + ", " + deserializedPerson.getAge());

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

}

}

}

Java IO 体系庞大且复杂,但掌握这些基础知识是深入学习的关键。通过合理运用不同类型的流,我们可以高效地处理各种 IO 任务,让程序与外部世界顺畅沟通。

相关推荐
IT·小灰灰10 分钟前
30行PHP,利用硅基流动API,网页客服瞬间上线
开发语言·人工智能·aigc·php
快点好好学习吧11 分钟前
phpize 依赖 php-config 获取 PHP 信息的庖丁解牛
android·开发语言·php
秦老师Q12 分钟前
php入门教程(超详细,一篇就够了!!!)
开发语言·mysql·php·db
是誰萆微了承諾12 分钟前
php 对接deepseek
android·开发语言·php
刚刚入门的菜鸟14 分钟前
php-curl
运维·web安全·php
vx_BS8133016 分钟前
【直接可用源码免费送】计算机毕业设计精选项目03574基于Python的网上商城管理系统设计与实现:Java/PHP/Python/C#小程序、单片机、成品+文档源码支持定制
java·python·课程设计
2601_9498683616 分钟前
Flutter for OpenHarmony 电子合同签署App实战 - 已签合同实现
java·开发语言·flutter
达文汐44 分钟前
【困难】力扣算法题解析LeetCode332:重新安排行程
java·数据结构·经验分享·算法·leetcode·力扣
培风图南以星河揽胜1 小时前
Java版LeetCode热题100之零钱兑换:动态规划经典问题深度解析
java·leetcode·动态规划
启山智软1 小时前
【中大企业选择源码部署商城系统】
java·spring·商城开发