java之IO流(三)

字节缓冲输出流:
  构造方式:
  (第一种开发中) public BufferedOutputStream(OutputStream out):采用的默认的缓冲区大小(足够大了) ,来构造一个字节缓冲输出流对象
   public BufferedOutputStream(OutputStream out,int size):指定size缓冲区大小构造缓冲输出流对象
   IllegalArgumentException - 如果 size <= 0 
  写数据的方式:
   一次写一个字节
   write(int by)
   一次写一个字节数组的一部分
   write(byte[] b, int off, int len) 
  方法:
  void flush() ;刷新缓冲区的流
  面试题:
   字节缓冲输出流它的构造方法为什么不能直接传递路径/文件?
   缓冲输入流/缓冲输出流,它只是在底层内部提供一个缓冲区的数组,
底层实现文件的复制、读取、写入这些操作都依赖于基本流对象来操作(InputStream/OutputStream/FileInputStream/FileOutputstream)
 举例:
public class BufferdOutputStreamDemo {
public static void main(String[] args) throws IOException {
//构造一个字符串缓冲区流对象
//	FileOutputStream out=new FileOutputStream("out.txt");
//	BufferedOutputStream bos=new BufferedOutputStream(out);
	//符合一种设计模式,装饰着设计模式,过滤器(Filter)
	BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("bos.txt"));
    bos.write("hello".getBytes());
    bos.close();  
}
}
字节缓冲输入流
   public BufferedInputStream(InputStream in):默认缓冲区大小构造缓冲输入流对象
   public BufferedInputStream(InputStream in,int size):指定缓冲区大小构造缓冲输入流对象 
public int read()
   public 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("bos.txt"));
     byte[] b=new byte[1024];
     int len=0;
     while ((len=bis.read(b))!=-1) {
    	 System.out.println(new String(b,0,len));
    	 }
     bis.close();
}
}
存储文件
  IO流:永久存储(耗时)
   数据库:永久存储
  基本的字节流
   文件字节输入流/文件字节输出流
  高效的字节流(缓冲流) 
  操作一个视频文件,来测试速度问题
  基本的字节流一次读取一个字节 ://共费时:78毫秒
  基本的字节流一次读取一个字节数组 :共耗时:16毫秒 
  高效的字节流一次读取一个字节      :共耗时:16毫秒
  高效的字节流一次读取一个字节数组:共耗时:0毫秒

 StringBuffer:提供了一个字符串缓冲区 (可以在缓冲区中不断追加字符串)

举例:

 
public class Demo {
public static void main(String[] args) throws IOException {
	long start=System.currentTimeMillis();
//	method1("mm.jpg","copy1.jpg");
//	method2("mm.jpg","copy2.jpg");
//	method3("mm.jpg","copy3.jpg");
	method4("mm.jpg","copy4.jpg");
	long end=System.currentTimeMillis();
	System.out.println("共费时:"+(end-start)+"毫秒");
	
}

private static void method4(String src, String dest) throws IOException {
	BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));
	BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dest));
	byte[] b=new byte[1024];
	int len=0;
	while((len=bis.read(b))!=-1) {
		bos.write(b,0,len);
	}
	bis.close();
	bos.close();
}

private static void method3(String src, String dest) throws IOException {
	BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));
	BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dest));
	int b=0;
	while((b=bis.read())!=-1) {
		bos.write(b);
	}
	bis.close();
	bos.close();
}

private static void method2(String src, String dest) throws IOException {
	FileInputStream fis=new FileInputStream(src);
	FileOutputStream fos=new FileOutputStream(dest);
	byte[] b=new byte[1024];
	int len=0;
	while((len=fis.read(b))!=-1) {
		fos.write(b,0,len);
	}
    fis.close();
    fos.close();
}


private static void method1(String src,String dest) throws IOException {
	FileInputStream fis=new FileInputStream(src);
	FileOutputStream fos=new FileOutputStream(dest);
	int by=0;
	while((by=fis.read())!=-1) {
		fos.write(by);
	}
	fis.close();
	fos.close();
}
}


猜你喜欢

转载自blog.csdn.net/wt5264/article/details/80501728