当前位置: 首页 > news >正文

Java 之 IO流

一、IO流概述

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

二、IO流的分类

  • 按流向分类

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

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

三、字节流详解

  • 字节输入流(InputStream)

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

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

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

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

四、字符流详解

  • 字符输入流(Reader)

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

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

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

// 示例:使用FileWriter向文件写入数据(字符方式)
FileWriter fw = null;
try {fw = new FileWriter("output.txt"); // 创建文件字符输出流对象String str = "Hello, Character Stream!"; // 待写入的字符串fw.write(str); // 向文件写入字符串
} catch (IOException e) {e.printStackTrace(); // 处理异常
} finally {if (fw != null) {try {fw.close(); // 关闭流,释放资源} catch (IOException e) {e.printStackTrace(); // 处理异常}}
}

五、缓冲流与高效IO

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

1. 缓冲输入流(BufferedInputStream)

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

2. 缓冲输出流(BufferedOutputStream)

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

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

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

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

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

六、对象流

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

ObjectInputStream

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

// 示例:使用ObjectInputStream读取对象
ObjectInputStream ois = null;
try {ois = new ObjectInputStream(new FileInputStream("object.dat")); // 创建对象输入流MyObject obj = (MyObject) ois.readObject(); // 从文件中读取对象System.out.println(obj); // 打印恢复的对象
} catch (IOException | ClassNotFoundException e) {e.printStackTrace(); // 处理异常
} finally {if (ois != null) {try {ois.close(); // 关闭流,释放资源} catch (IOException e) {e.printStackTrace(); // 处理异常}}
}

ObjectOutputStream

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

// 示例:使用ObjectOutputStream写入对象
ObjectOutputStream oos = null;
try {oos = new ObjectOutputStream(new FileOutputStream("object.dat")); // 创建对象输出流MyObject obj = new MyObject(); // 创建待序列化的对象oos.writeObject(obj); // 将对象写入文件
} catch (IOException e) {e.printStackTrace(); // 处理异常
} finally {if (oos != null) {try {oos.close(); // 关闭流,释放资源} catch (IOException e) {e.printStackTrace(); // 处理异常}}
}

七、文件操作辅助类

File类

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

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

Files类(Java NIO)

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

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

八、总结

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

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

http://www.lryc.cn/news/440819.html

相关文章:

  • 计算机毕业设计 健身房管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • uniapp uview扩展u-picker支持日历期间 年期间 月期间 时分期间组件
  • GAMES101(10~11节,几何)
  • 家电制造的隐形守护者:矫平机确保材料完美无瑕
  • 软件设计师考纲及笔记
  • 基于SpringCloud的微服务框架
  • C++速通LeetCode简单第19题-只出现一次的数字
  • AutoSar AP中Proxy Class中Methods描述的总结
  • 如何在本地计算机中打开远程服务器的Jupyter notebook
  • 实习项目|苍穹外卖|day11
  • easy_cloudantivirus
  • python中Web API 框架
  • Linux(6)--CentOS目录
  • 标准C++(二)
  • 硬件工程师笔试面试——保险丝
  • 本地安装Ollama+WebUI
  • 请求响应-05.请求-日期参数JSON参数
  • CSS学习路线
  • Linux memcg lru lock提升锁性能
  • 【spring】引入 Jackson 依赖 对java对象序列号和反序列化
  • 算法面经手撕系列(3)--手撕LayerNormlization
  • 出厂非澎湃OS手机解BL锁
  • Go语言错误处理之道:优雅地应对程序中的问题
  • LIMS实验室管理系统的特点
  • vue之 package.json和package-lock.json
  • android 老项目中用到的jar包不存在,通过离线的方法加载
  • 每天五分钟玩转深度学习框架PyTorch:梯度下降之学习率衰减
  • 智能家居配上高颜值UI,瞬间感觉消费不起了呢
  • Winform登录实现及工具栏切换
  • Git bash使用