JavaSE|IO流:FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream


IO流用来处理设备之间的数据传输。
Java对数据的操作是通过流的方式。
Java用于操作流的对象都在IO包中。

IO流分类

  • 按照数据流向
    – 输入流 读入数据
    – 输出流 写出数据
  • 按照数据类型
    – 字节流:字节输入流 InputStream; 字节输出流 OutputStream
    – 字符流:字符输入流 Reader; 字符输出流 Writer

字节流:

  • InputStream
    – FileInputStream
    – BufferedInputStream
  • OutputStream
    – FileOutputStream
    – BufferedOutputStream

字符流:

  • Reader
    – FileReader
    – BufferedReader
  • Writer
    – FileWriter
    – BufferedWriter

如何选择使用字节流还是字符流?
如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。如果你什么都不知道,就用字节流。

PS:一般在探讨IO流的时候,如果没有明确说明按哪种分类来说,默认情况下是按照数据类型来分的。

FileOutputStream

构造方法

  1. FileOutputStream (File file)
  2. FileOutputStream (String name)
		// 创建字节输出流对象
		// FileOutputStream(File file)
		// File file = new File("fos.txt");
		// FileOutputStream fos = new FileOutputStream(file);
		// FileOutputStream(String name)
		FileOutputStream fos = new FileOutputStream("fos.txt");
		/*
		 * 创建字节输出流对象了做了几件事情:
		 * A:调用系统功能去创建文件
		 * B:创建fos对象
		 * C:把fos对象指向这个文件
		 */
		
		//写数据
		fos.write("hello,IO".getBytes());
		fos.write("java".getBytes());
		
		//释放资源
		//关闭此文件输出流并释放与此流有关的所有系统资源。
		fos.close();
		/*
		 * 为什么一定要close()呢?
		 * A:让流对象变成垃圾,这样就可以被垃圾回收器回收了
		 * B:通知系统去释放跟该文件相关的资源
		 */
		//java.io.IOException: Stream Closed
		//fos.write("java".getBytes());
  1. FileOutputStream(File file, boolean append)
  2. FileOutputStream(String name, boolean append)

成员方法

  1. public void write(int b)
  2. public void write(byte[] b)
  3. public void write(byte[] b,int off,int len)
		FileOutputStream fos = new FileOutputStream("fos2.txt");

		// 调用write()方法
		fos.write(97); //97 -- 底层二进制数据	-- 通过记事本打开 -- 找97对应的字符值 -- a
		fos.write(57); //9
		fos.write(55); //7
		
		//public void write(byte[] b):写一个字节数组
		byte[] bys={97,98,99,100,101};
		fos.write(bys); //abcde
		
		//public void write(byte[] b,int off,int len):写一个字节数组的一部分
		fos.write(bys,1,3); //bcd
		
		//释放资源
		fos.close();

常见问题

  1. 创建字节输出流到底做了哪些事情?
    1)调用系统功能去创建文件
    2)创建fos对象
    3)把fos对象指向这个文件
  2. 数据写成功后,为什么要close()?
    1)让流对象变成垃圾,这样就可以被垃圾回收器回收了
    2)通知系统去释放跟该文件相关的资源
  3. 如何实现数据的换行?
    写入换行符号既可。但不同系统针对不同换行符号的识别是不一样的。
    Windows: \r\n
    linux: \n
    Mac: \r
    而一些高级记事本,是可以识别任意换行符的。
  4. 如何实现数据的追加写入?
    用构造方法带第二个参数是true的情况既可。

加入异常处理

public class FileOutputStreamDemo4 {
	public static void main(String[] args) {
		// 分开做异常处理
		// FileOutputStream fos = null;
		// try {
		// fos = new FileOutputStream("fos4.txt");
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// }
		//
		// try {
		// fos.write("java".getBytes());
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		//
		// try {
		// fos.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// 一起做异常处理
		// try {
		// FileOutputStream fos = new FileOutputStream("fos4.txt");
		// fos.write("java".getBytes());
		// fos.close();
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// 改进版
		// 为了在finally里面能够看到该对象就必须定义到外面,为了访问不出问题,还必须给初始化值
		FileOutputStream fos = null;
		try {
			// fos = new FileOutputStream("z:\\fos4.txt");
			fos = new FileOutputStream("fos4.txt");
			fos.write("java".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 如果fos不是null,才需要close()
			if (fos != null) {
				// 为了保证close()一定会执行,就放到这里了
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

FileInputStream

把刚才写的数据读取出来显示在控制台。

构造方法

  1. FileInputStream(File file)
  2. FileInputStream(String name)

成员方法

  1. public int read()
    从此输入流中读取一个数据字节。返回下一个数据字节;如果已到达文件末尾,则返回 -1。
		FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");

		// 用循环改进
		// int by = fis.read();
		// while (by != -1) {
		// System.out.print((char) by);
		// by = fis.read();
		// }

		// 最终版代码
		int by = 0;
		// 读取,赋值,判断
		while ((by = fis.read()) != -1) {
			System.out.print((char) by);
		}

		// 释放资源
		fis.close();
  1. public int read(byte[] b)
    从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。返回读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
		// 创建字节输入流对象
		// FileInputStream fis = new FileInputStream("fis2.txt");
		FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");

		// byte[] bys = new byte[115]; // 0
		// int len = 0;
		// while ((len = fis.read(bys)) != -1) {
		// System.out.print(new String(bys, 0, len));
		// // System.out.print(new String(bys)); //千万要带上len的使用
		// }

		// 最终版代码
		// 数组的长度一般是1024或者1024的整数倍
		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			System.out.print(new String(bys, 0, len));
		}

		// 释放资源
		fis.close();

字节缓冲流

字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,这是加入了数组这样的缓冲区效果,java本身在设计的时候,也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流。

BufferedOutputStream

该类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。

构造方法

  1. BufferedOutputStream(OutputStream out)
    创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
    一般用这个默认大小的就可以。
  2. BufferedOutputStream(OutputStream out, int size)
    创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。
	BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));
	bos.write("hello);
	bos.close();

BufferedInputStream

构造方法

  1. BufferedInputStream(InputStream in)
    创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
  2. BufferedInputStream(InputStream in, int size)
    创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
	BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt"));
	
	byte[] bys = new byte[1024];
	int len = 0;
	while((len = bis.read(bys)) != -1 ){
		System.out.println(new String(bys, 0, len));
	}
	bis.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值