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

带缓存的输入/输出流

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();
				}
			}
		}

	}
}

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

猜你喜欢

转载自blog.csdn.net/javanofa/article/details/104477050