字节流
IO流概述和分类
- 分类
- 按照数据的流向
- 输入流:读数据
- 输出流:写数据
- 按照数据类型来分 (默认)
- 字节流
- 字节输入流;字节输出流
- 字符输入流;字符输出流
- 字节流
- 按照数据的流向
1.字节输入流
- InputStream 父类
-
FileInputStream
-
构造方法:
- FileInputStream file = new FileInputStream(“文件路径”);
- FileInputStream file = new FileInputStream(File file);
-
获取方法:
方法名 描述 close() 关闭此输入流并释放与流相关联的任何系统资源。 read() 一次读取一个字节数据 read(byte[] b) 从该输入流读取最多 b.length个字节的数据到一个字节数组。 read(byte[] b, int off, int len) 一次读取一个字节数组指定长度数据 方式一 FileInputStream fis = new FileInputStream("文件路径"); int len=fis.read() 一次读取一个字节 len代表读取到的一个字节 方式二 一次读取一个字节数组 byte[] arr =new byte[1024] int len=fis.read(arr); len代表读取到的字节数组里面的个数
-
-
BufferedInputStream
-
构造方法:
BufferedInputStream br = new BufferedInputStream(new FileInputStream(“文件路径”)); -
获取方法:
方法名 描述 close() 关闭此输入流并释放与流相关联的任何系统资源。 read() 一次读取一个字节 read(byte[] b, int off, int len) 一次读取一个字节数组 方式一 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("文件路径") ); int len=bis.read() 一次读取一个字节 len代表读取到的一个字节 方式二 一次读取一个字节数组 byte[] arr =new byte[1024] int len=br.read(arr); len代表读取到的字节数组里面的个数 br.read(arr)
-
-
2.字节输出流
- OutputStream 父类
-
FileOutputStream
-
构造方法:
- FileOutputStream file = FileOutputStream(“文件路径”);
- FileOutputStream file = FileOutputStream(File file);
- FileOutputStream(String name, boolean append) 在文件后可追加内容
-
获取方法:
方法名 描述 close() 关闭此输入流并释放与流相关联的任何系统资源。 write(int b) 一次写入一个字节 write(byte[] b, int off, int len) 一次写入一个字节数组的一部分或全部 方式一 FileOutputStream fos = new FileOutputStream("文件路径"); fos.write() 方式二 一次写一个字节数组 byte[] arr =new byte[1024] int len=br.read(arr); len代表读取到的字节数组里面的个数 fos.write(char[],0,len)
-
-
BufferedOutputStream
-
BufferedOutputStream br = new BufferedOutputStream(new FileOutputStream(“文件路径”));
-
获取方法:
方法名 描述 flush() 刷新缓冲输出流。 write(int b) 一次写入一个字节 write(byte[] b, int off, int len) 将指定的字节写入缓冲的输出流。
-
-