Java的I/O流 —— 带缓存的输入/输出流

本文介绍了Java中带缓存的输入/输出流,包括BufferedInputStream和BufferedOutputStream类,以及BufferedReader和BufferedWriter类。缓存流通过在I/O操作中引入内存缓存区,显著提升了数据传输的效率。文中通过实例展示了未使用缓存和使用缓存的性能对比,并提供了使用缓存字符流进行行级输入/输出的代码示例。
摘要由CSDN通过智能技术生成

带缓存的输入/输出流

BufferedInputStrean 与 BufferedOutputStream类

缓存是I/O的一种性能优化,缓存流为I/O流增加了内存缓存区,有了缓存区,可以大大提升运行效率。

举个例子,现在有A,B两个点,A点有货物需要搬到B点,如果采用人工搬运的方式,一次只可以搬运一个货物,效率非常低。
在这里插入图片描述
如果我们换一种方式,开一辆车,如果这车足够大的话,可以把货物全部装下,一次运到B点,这大大提高了工作效率。
在这里插入图片描述
缓存区的作用就相当于这个车。缓存输入/输出流的使用方法与文件输入/输出流的使用方法很像,缓存输入/输出流,嵌套在文件输入/输出流中。

举个例子,我要读取我位于桌面的Java API,的数据
在这里插入图片描述
代码如下:

public class Dome {

	public static void main(String[] args) {

		// 创建文件对象,文件为我位于桌面上的Java,api
		File f = new File("C:\\Users\\逢青\\Desktop\\jdk api 1.8.CHM");

		FileInputStream in = null;

		long start = System.currentTimeMillis();// 程序开始时间
		try {
			in = new FileInputStream(f);// 创建FileInputStream对象
			byte b[] = new byte[1024];// 创建byte数组储存读取的信息
			while (in.read(b) != -1) {// 循环遍历输出,读取信息

			}
			long end = System.currentTimeMillis();// 程序结束时间
			System.out.println("运行经历毫秒值:" + (end - start));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {// 判断in是否为空值,如果不是则关闭流
				try {
					in.close();// 关闭流
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}

这是没有添加缓存区的,用了143毫秒
在这里插入图片描述
我们添加缓存区

public class Dome {

	public static void main(String[] args) {

		// 创建文件对象,文件为我位于桌面上的Java,api
		File f = new File("C:\\Users\\逢青\\Desktop\\jdk api 1.8.CHM");

		FileInputStream in = null;
		BufferedInputStream bi = null;

		long start = System.currentTimeMillis();// 程序开始时间
		try {
			in = new FileInputStream(f);// 创建FileInputStream对象
			bi = new BufferedInputStream(in);//创建BufferedInputStream对象
			byte b[] = new byte[1024];// 创建byte数组储存读取的信息
			while (bi.read(b) != -1) {// 循环遍历输出,读取信息

			}
			long end = System.currentTimeMillis();// 程序结束时间
			System.out.println("运行经历毫秒值:" + (end - start));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bi != null) {// 判断bi是否为空值,如果不是则关闭流
				try {
					bi.close();// 关闭流
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (in != null) {// 判断in是否为空值,如果不是则关闭流
				try {
					in.close();// 关闭流
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}

效率提升的一半多,用了35毫秒
在这里插入图片描述

BufferedReader 与 BufferedWriter类

缓存字符流BufferedReader与BufferedWriter最大的特点是可以以行为单位进行输入/输出

方法作用
readLine()读取一行文本
newLine()写入时创建新行

写个例子:

public class Study2 {

	public static void main(String[] args) {

		File f = new File("word.txt");// 创建文件对象

		FileWriter fw = null;
		BufferedWriter bw = null;

		try {
			fw = new FileWriter(f);// 创建FileWriter对象
			bw = new BufferedWriter(fw);// 将文件字符流包装成缓存字符流

			String str1 = "抽刀断水水更流";
			String str2 = "举杯消愁愁更愁";
			bw.write(str1);// 写入第一句话
			bw.newLine();// 创建新行,也就是换行
			bw.write(str2);// 写入第二句话

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {// 判断bw是否为空值,如果不是则关闭流
				try {
					bw.close();// 关闭流
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fw != null) {// 判断fw是否为空值,如果不是则关闭流
				try {
					fw.close();// 关闭流
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}

实现了在写入的时候换行了
在这里插入图片描述
反过来也是可以的,在txt文件中写几行话。在这里插入图片描述
代码如下:

public class Study2 {

	public static void main(String[] args) {

		File f = new File("word.txt");// 创建文件对象

		FileReader fr = null;
		BufferedReader br = null;

		try {
			fr = new FileReader(f);// 创建FileReader对象
			br = new BufferedReader(fr);// 包装FileReader对象
			String tmp = null;
			int i = 1;// 计数器
			while ((tmp = br.readLine()) != null) {// 循环读取文件中的信息
				System.out.println("第" + i + "行:" + tmp);
				i++;
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {// 判断br是否为空值,如果不是则关闭流
				try {
					br.close();// 关闭流
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fr != null) {// 判断fr是否为空值,如果不是则关闭流
				try {
					fr.close();// 关闭流
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}

运行效果截图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值