• Java 之 IO流


    一、IO流概述

    在计算机编程中,IO流(Input/Output Stream)是处理设备间数据传输的关键技术。简单来说,IO流就是以流的方式进行输入输出,数据被当作无结构的字节序或字符序列来处理。在Java等编程语言中,IO流用于操作数据的类都集中在IO包中。

    二、IO流的分类

    • 按流向分类

      • 输入流:用于从数据源(如文件、键盘)读取数据。
      • 输出流:用于向目标(如文件、屏幕)写入数据。
    • 按操作类型分类

      • 字节流:以字节为单位进行操作,适用于所有类型的数据。
      • 字符流:以字符为单位进行操作,更便于处理文本数据。

    三、字节流详解

    • 字节输入流(InputStream)

    FileInputStream:从文件中读取数据的常用类。

    1. // 示例:使用FileInputStream读取文件内容
    2. FileInputStream fis = null;
    3. try {
    4. fis = new FileInputStream("example.txt"); // 创建文件输入流对象
    5. int content;
    6. while ((content = fis.read()) != -1) { // 逐个字节读取文件内容
    7. System.out.print((char) content); // 输出读取到的字节(转换为字符)
    8. }
    9. } catch (IOException e) {
    10. e.printStackTrace(); // 处理异常
    11. } finally {
    12. if (fis != null) {
    13. try {
    14. fis.close(); // 关闭流,释放资源
    15. } catch (IOException e) {
    16. e.printStackTrace(); // 处理异常
    17. }
    18. }
    19. }
    • 字节输出流(OutputStream)

    FileOutputStream:向文件中写入数据的常用类。

    1. // 示例:使用FileOutputStream向文件写入数据
    2. FileOutputStream fos = null;
    3. try {
    4. fos = new FileOutputStream("output.txt"); // 创建文件输出流对象
    5. String str = "Hello, World!"; // 待写入的字符串
    6. byte[] bytes = str.getBytes(); // 将字符串转换为字节数组
    7. fos.write(bytes); // 向文件写入字节数组
    8. } catch (IOException e) {
    9. e.printStackTrace(); // 处理异常
    10. } finally {
    11. if (fos != null) {
    12. try {
    13. fos.close(); // 关闭流,释放资源
    14. } catch (IOException e) {
    15. e.printStackTrace(); // 处理异常
    16. }
    17. }
    18. }

    四、字符流详解

    • 字符输入流(Reader)

    FileReader:从文件中读取字符数据的常用类。

    1. // 示例:使用FileReader读取文件内容(字符方式)
    2. FileReader fr = null;
    3. try {
    4. fr = new FileReader("example.txt"); // 创建文件字符输入流对象
    5. int c;
    6. while ((c = fr.read()) != -1) { // 逐个字符读取文件内容
    7. System.out.print((char) c); // 输出读取到的字符
    8. }
    9. } catch (IOException e) {
    10. e.printStackTrace(); // 处理异常
    11. } finally {
    12. if (fr != null) {
    13. try {
    14. fr.close(); // 关闭流,释放资源
    15. } catch (IOException e) {
    16. e.printStackTrace(); // 处理异常
    17. }
    18. }
    19. }
    • 字符输出流(Writer)

    FileWriter:向文件中写入字符数据的常用类。

    1. // 示例:使用FileWriter向文件写入数据(字符方式)
    2. FileWriter fw = null;
    3. try {
    4. fw = new FileWriter("output.txt"); // 创建文件字符输出流对象
    5. String str = "Hello, Character Stream!"; // 待写入的字符串
    6. fw.write(str); // 向文件写入字符串
    7. } catch (IOException e) {
    8. e.printStackTrace(); // 处理异常
    9. } finally {
    10. if (fw != null) {
    11. try {
    12. fw.close(); // 关闭流,释放资源
    13. } catch (IOException e) {
    14. e.printStackTrace(); // 处理异常
    15. }
    16. }
    17. }

    五、缓冲流与高效IO

    为了提高IO操作的效率,Java提供了缓冲流(BufferedStream),包括BufferedInputStream、BufferedOutputStream、BufferedReader和BufferedWriter。这些缓冲流在读写操作中添加了缓冲区。

    1. 缓冲输入流(BufferedInputStream)

    • 通过在内存中使用缓冲区来提高从文件中读取数据的效率。
    1. // 示例:使用BufferedInputStream提高文件读取效率
    2. BufferedInputStream bis = null;
    3. try {
    4. bis = new BufferedInputStream(new FileInputStream("example.txt")); // 创建带缓冲的输入流对象
    5. int content;
    6. while ((content = bis.read()) != -1) { // 从缓冲区读取数据
    7. System.out.print((char) content); // 输出读取到的字节(转换为字符)
    8. }
    9. } catch (IOException e) {
    10. e.printStackTrace(); // 处理异常
    11. } finally {
    12. if (bis != null) {
    13. try {
    14. bis.close(); // 关闭流,释放资源
    15. } catch (IOException e) {
    16. e.printStackTrace(); // 处理异常
    17. }
    18. }
    19. }

    2. 缓冲输出流(BufferedOutputStream)

    • 通过缓冲区在内存中积累数据,提高写入文件的效率。
    1. // 示例:使用BufferedOutputStream提高文件写入效率
    2. BufferedOutputStream bos = null;
    3. try {
    4. bos = new BufferedOutputStream(new FileOutputStream("output.txt")); // 创建带缓冲的输出流对象
    5. String str = "Hello, Buffered Output Stream!"; // 待写入的字符串
    6. byte[] bytes = str.getBytes(); // 将字符串转换为字节数组
    7. bos.write(bytes); // 向缓冲区写入数据
    8. } catch (IOException e) {
    9. e.printStackTrace(); // 处理异常
    10. } finally {
    11. if (bos != null) {
    12. try {
    13. bos.close(); // 关闭流,释放资源
    14. } catch (IOException e) {
    15. e.printStackTrace(); // 处理异常
    16. }
    17. }
    18. }

    3. 缓冲字符输入流(BufferedReader)

    • 通过缓存字符数据提高读取文本数据的效率。
    1. // 示例:使用BufferedReader读取文件内容(字符方式)
    2. BufferedReader br = null;
    3. try {
    4. br = new BufferedReader(new FileReader("example.txt")); // 创建带缓冲的字符输入流对象
    5. String line;
    6. while ((line = br.readLine()) != null) { // 逐行读取文本数据
    7. System.out.println(line); // 输出读取到的每一行
    8. }
    9. } catch (IOException e) {
    10. e.printStackTrace(); // 处理异常
    11. } finally {
    12. if (br != null) {
    13. try {
    14. br.close(); // 关闭流,释放资源
    15. } catch (IOException e) {
    16. e.printStackTrace(); // 处理异常
    17. }
    18. }
    19. }

    4. 缓冲字符输出流(BufferedWriter)

    • 使用缓冲区提高写入文本数据的效率。
    1. // 示例:使用BufferedWriter向文件写入数据(字符方式)
    2. BufferedWriter bw = null;
    3. try {
    4. bw = new BufferedWriter(new FileWriter("output.txt")); // 创建带缓冲的字符输出流对象
    5. String str = "Hello, Buffered Writer!"; // 待写入的字符串
    6. bw.write(str); // 向缓冲区写入数据
    7. bw.newLine(); // 添加新行
    8. bw.write("Second Line"); // 写入第二行
    9. } catch (IOException e) {
    10. e.printStackTrace(); // 处理异常
    11. } finally {
    12. if (bw != null) {
    13. try {
    14. bw.close(); // 关闭流,释放资源
    15. } catch (IOException e) {
    16. e.printStackTrace(); // 处理异常
    17. }
    18. }
    19. }

    六、对象流

    对象流用于处理对象的序列化和反序列化,即将对象转换为字节流保存到文件中,或从字节流中恢复对象。

    ObjectInputStream

    反序列化对象(从字节流恢复对象)。

    1. // 示例:使用ObjectInputStream读取对象
    2. ObjectInputStream ois = null;
    3. try {
    4. ois = new ObjectInputStream(new FileInputStream("object.dat")); // 创建对象输入流
    5. MyObject obj = (MyObject) ois.readObject(); // 从文件中读取对象
    6. System.out.println(obj); // 打印恢复的对象
    7. } catch (IOException | ClassNotFoundException e) {
    8. e.printStackTrace(); // 处理异常
    9. } finally {
    10. if (ois != null) {
    11. try {
    12. ois.close(); // 关闭流,释放资源
    13. } catch (IOException e) {
    14. e.printStackTrace(); // 处理异常
    15. }
    16. }
    17. }

    ObjectOutputStream

    序列化对象(将对象写入字节流)。

    1. // 示例:使用ObjectOutputStream写入对象
    2. ObjectOutputStream oos = null;
    3. try {
    4. oos = new ObjectOutputStream(new FileOutputStream("object.dat")); // 创建对象输出流
    5. MyObject obj = new MyObject(); // 创建待序列化的对象
    6. oos.writeObject(obj); // 将对象写入文件
    7. } catch (IOException e) {
    8. e.printStackTrace(); // 处理异常
    9. } finally {
    10. if (oos != null) {
    11. try {
    12. oos.close(); // 关闭流,释放资源
    13. } catch (IOException e) {
    14. e.printStackTrace(); // 处理异常
    15. }
    16. }
    17. }

    七、文件操作辅助类

    File类

    提供文件和目录的操作方法。

    1. // 示例:使用File类进行文件操作
    2. File file = new File("example.txt"); // 创建File对象
    3. if (file.exists()) {
    4. System.out.println("文件存在");
    5. System.out.println("文件大小: " + file.length() + " bytes"); // 获取文件大小
    6. System.out.println("是否是文件: " + file.isFile()); // 判断是否为文件
    7. System.out.println("是否是目录: " + file.isDirectory()); // 判断是否为目录
    8. } else {
    9. System.out.println("文件不存在");
    10. }

    Files类(Java NIO)

    提供更高效的文件操作方法。

    1. // 示例:使用Files类读取文件内容
    2. Path path = Paths.get("example.txt"); // 创建Path对象
    3. try {
    4. List lines = Files.readAllLines(path); // 读取所有行
    5. for (String line : lines) {
    6. System.out.println(line); // 输出每一行
    7. }
    8. } catch (IOException e) {
    9. e.printStackTrace(); // 处理异常
    10. }

    八、总结

    IO流是处理数据读写的基础。Java中的IO流分为字节流和字符流,字节流适合处理各种数据类型,而字符流则专门处理文本数据。为了提高IO效率,Java提供了缓冲流。此外,对象流允许对对象进行序列化和反序列化操作。通过File类和Files类可以方便地进行文件操作。

    通过掌握以上知识,你可以在实际开发中更有效地处理数据输入输出操作,优化程序性能。希望能帮助各位看官更深入理解Java IO流的使用,感谢各位看官的观看,下期见,谢谢~

  • 相关阅读:
    绝地求生:PCL2024春季赛第二轮D2:MnG极致运营,206分登顶榜首
    adb shell pm path packageName
    【更新】囚生CYの备忘录(202331014~)
    C++模板初阶
    java计算机毕业设计云端小区物业智能管理系统MyBatis+系统+LW文档+源码+调试部署
    Java基础知识篇之类的基本概念
    宝塔部署项目添加域名配置证书 ERR_TOO_MANY_REDIRECTS 重定向次数过多
    2.2.2同向放大器、同向放大器的设计
    I Chiitoitsu
    如何设计一个完美的笔记本电脑
  • 原文地址:https://blog.csdn.net/weixin_64178283/article/details/142306584