本文共 4457 字,大约阅读时间需要 14 分钟。
流在编程中是一个非常重要的概念,它用来描述数据在程序中的传输和存储方式。流就是像水流一样,从一端流向另一端。在编程中,数据也以流的形式进行传输或保存。程序在运行时,需要读取数据或将数据输出,这都需要通过流来实现。
根据流的方向,流可以分为输入流和输出流:
根据流的传输单位,流可以分为字节流和字符流:
所有文件在存储时都是以字节形式保存的,磁盘上保存的是字节,而不是字符。读取文件时,必须将字节转换为字符才能显示或处理。这表明,文件操作的核心实际上是字节流的操作。
字节流和字符流之间的转换可以通过InputStreamReader和OutputStreamWriter实现。这些转换器将字节流和字符流连接起来,允许程序在处理数据时灵活选择使用字节流还是字符流。
字节流用于处理所有类型的数据,包括二进制文件。常用的字节流类有FileInputStream和FileOutputStream,它们分别用于读取和写入文件。以下是一个字节流操作的示例:
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class IoDemo4 { public static void main(String[] args) throws IOException { String srcFilePath = "D:\\io_test\\加菲猫.jpg"; String destFilePath = "D:\\io_test\\加菲猫2.jpg"; FileInputStream fileInputStream = new FileInputStream(srcFilePath); FileOutputStream fileOutputStream = new FileOutputStream(destFilePath); byte[] bytes = new byte[1024]; try { int count = 0; while ((count = fileInputStream.read(bytes)) != -1) { fileOutputStream.write(bytes, 0, count); } } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { fileInputStream.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } }} 使用缓冲区可以提高IO操作的性能。缓冲区将数据在内存中暂存,减少磁盘操作的次数。以下是一个带缓冲区的字节流操作示例:
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;public class IoDemo5 { public static void main(String[] args) { String srcFilePath = "D:\\io_test\\加菲猫.jpg"; String destFilePath = "D:\\io_test\\加菲猫3.jpg"; try { BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFilePath)); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFilePath)); byte[] bytes = new byte[1024]; int count = 0; while ((count = bufferedInputStream.read(bytes)) != -1) { bufferedOutputStream.write(bytes, 0, count); } } catch (Exception e) { e.printStackTrace(); } }} 字符流专门用于处理文本数据。字符流的基本类是Reader和Writer。以下是一个字符流操作的示例:
import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class IoDemo7 { public static void main(String[] args) throws IOException { String filePath = "D:\\io_test\\1\\text.txt"; String content = "欢迎来到Java~"; FileWriter fileWriter = new FileWriter(filePath); try { fileWriter.write(content); } catch (IOException e) { e.printStackTrace(); } finally { fileWriter.close(); } }} 字符流也可以使用缓冲区来提高性能。以下是一个带缓冲区的字符流操作示例:
import java.io.BufferedReader;import java.io BufferedWriter;import java.io FileReader;import java.io FileWriter;import java.io.IOException;public class IoDemo10 { public static void main(String[] args) throws IOException { String srcFilePath = "D:\\io_test\\1\\tt.txt"; String destFilePath = "D:\\io_test\\1\\tt2.txt"; try { BufferedReader bufferedReader = new BufferedReader(new FileReader(srcFilePath)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFilePath)); char[] chars = new char[1024]; while (true) { int count = bufferedReader.read(chars); if (count == -1) { break; } bufferedWriter.write(chars, 0, count); } } catch (IOException e) { e.printStackTrace(); } finally { bufferedReader.close(); bufferedWriter.close(); } }} 通过实际练习字节流和字符流的操作,我对它们的使用更加熟悉,也明白了在实际开发中如何根据需求选择合适的IO操作流类型。
转载地址:http://xgtaz.baihongyu.com/