博客名称 |
---|
Java-(中级) |
文章目录
IO-BufferedIn(Out)putStream
java.io.InputStream 继承 java.io.FilterInputStream 继承 java.io.BufferedInputStream
java.io.OutputStream 继承 java.io.FilterOutputStream 继承 java.io.BufferedOutputStream
通过定义数组的方式确实比以前一次读取一个字节的方式快很多,所以,看来有一个缓冲区还是非常好的。
既然是这样的话,那么,java开始在设计的时候,它也考虑到了这个问题,就专门提供了带缓冲区的字节类。
这种类被称为:缓冲区类(高效类)
写数据:BufferedOutputStream
读数据:BufferedInputStream
BufferedOutputStream-(成员)构造方法
构造方法:
BufferedOutputStream(OutputStream out) :创建一个指定输出流的高效缓冲区
BufferedOutputStream(OutputStream out, int size):创建一个指定输出流的高效缓冲区,并指定缓冲区大小。
成员方法
public void write(byte[] b, int off, int len) :写一个字节数组一部分
public void write(int b) :写一个字节
构造方法可以指定缓冲区的大小,但是我们一般用不上,因为默认缓冲区大小就足够了。
为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?
原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。
类似于:杯子和水
代码演示
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException {
//创建字节输出流高效对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.txt"));
//写数据
bos.write("hello,world".getBytes());
//释放资源
bos.close();
}
}
BufferedInputStream-(成员)构造方法
构造方法:
BufferedInputStream(InputStream in) :创建一个指定输入流的高效缓冲区
BufferedInputStream(InputStream in, int size) :创建一个指定输入流的高效缓冲区,并指定缓冲区大小。
成员方法
int read() :读一个字节
int read(byte[] b, int off, int len):读一个字节数组的一部分
代码演示:
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
//创建字节输入流高效对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
//一次读取一个字节
/*int by = 0;
while ((by=bis.read())!=-1){
System.out.print((char)by);
}*/
//一次读取一个字节数组
byte[] bys = new byte[1024];
int len = 0;
while ((len=bis.read(bys))!=-1){
System.out.println(new String(bys,0,len));
}
// 释放资源
bis.close();
}
}
结果:
hello,world
BufferedIn(Out)putStream-测试四种复制map4的效率
需求:把D盘的周杰伦-霍元甲 (《霍元甲》电影主题曲)(高清).mp4复制到E盘下。
字节流四种方式复制文件:
基本字节流一次读写一个字节: 共耗时:157765毫秒
基本字节流一次读写一个字节数组: 共耗时:250毫秒
高效字节流一次读写一个字节: 共耗时:539毫秒
高效字节流一次读写一个字节数组: 共耗时:100毫秒
代码演示
public class CopyMVDemo {
public static void main(String[] args) {
long mill1 = System.currentTimeMillis();
//字节流读取,一次读取一个字节
method1("D:\\周杰伦-霍元甲 (《霍元甲》电影主题曲)(高清).mp4", "E:\\周杰伦-霍元甲1.mp4");
long mill2 = System.currentTimeMillis();
System.out.println("字节流读取,读一个字节耗时" + (mill2 - mill1) + "毫秒");
long mill3 = System.currentTimeMillis();
//字节流读取,一次读取一个字节数组
method2("D:\\周杰伦-霍元甲 (《霍元甲》电影主题曲)(高清).mp4", "E:\\周杰伦-霍元甲2.mp4");
long mill4 = System.currentTimeMillis();
System.out.println("字节流读取,读一个字节数组耗时" + (mill4 - mill3) + "毫秒");
long mill5 = System.currentTimeMillis();
//字节高效流读取,一次读取一个字节
method3("D:\\周杰伦-霍元甲 (《霍元甲》电影主题曲)(高清).mp4", "E:\\周杰伦-霍元甲3.mp4");
long mill6 = System.currentTimeMillis();
System.out.println("字节高效流读取,读一个字节耗时" + (mill6 - mill5) + "毫秒");
long mill7 = System.currentTimeMillis();
//字节高效流读取,一次读取字节数组
method4("D:\\周杰伦-霍元甲 (《霍元甲》电影主题曲)(高清).mp4", "E:\\周杰伦-霍元甲4.mp4");
long mill8 = System.currentTimeMillis();
System.out.println("字节高效流读取,读一个字节数组耗时" + (mill8 - mill7) + "毫秒");
}
//字节流读取,一次读取一个字节
public static void method1(String srcFolder, String tarFolder) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//创建字节输入流对象
fis = new FileInputStream(srcFolder);
//创建字节输出流对象
fos = new FileOutputStream(tarFolder);
//一次读取一个字节
int by = 0;
//读数据
while ((by = fis.read()) != -1) {
//写数据
fos.write(by);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//字节流读取,一次读取一个字节数组
public static void method2(String srcFolder, String tarFolder) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//创建字节输入流
fis = new FileInputStream(srcFolder);
//创建字节输出流
fos = new FileOutputStream(tarFolder);
//一次读取一个字节数组
byte[] bys = new byte[1024];
//返回实际读取长度
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//字节高效流读取,一次读取一个字节
public static void method3(String srcFolder, String tarFolder) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//创建字节输入高效流对象
bis = new BufferedInputStream(new FileInputStream(srcFolder));
//创建字节输出高效流对象
bos = new BufferedOutputStream(new FileOutputStream(tarFolder));
//字节高效流,一次读取一个字节
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//字节高效流读取,一次读取字节数组
public static void method4(String srcFolder, String tarFolder) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//创建字节输入高效流对象
bis = new BufferedInputStream(new FileInputStream(srcFolder));
//创建字节输出高效流对象
bos = new BufferedOutputStream(new FileOutputStream(tarFolder));
//字节高效流,一次读取一个字节数组
byte[] bys = new byte[1024];
//返回实际读取长度
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
结果:
字节流读取,读一个字节耗时157765毫秒
字节流读取,读一个字节数组耗时-250毫秒
字节高效流读取,读一个字节耗时-539毫秒
字节高效流读取,读一个字节数组耗时-100毫秒