zoukankan      html  css  js  c++  java
  • Java—缓冲流

    一、缓冲流

    高效流,一般指默认声明了一个byte空间(容器)作为缓冲区,比我们一般申请的空间64k大;所以较为高效。

    若我们要使FileInputStream流高效,把空间申请得最大即可实现。


    二、字节缓冲流

    2.1 字节缓冲输出流BufferedOutputStream

    public class BufferedStreamDemo {
    	public static void main(String[] args) throws IOException {
    		// 写操作
    		write();
    	}
    
    	public static void write() throws IOException {
    		// 创建流
    		OutputStream os = new FileOutputStream("e:/text.txt");
    		BufferedOutputStream bos = new BufferedOutputStream(os);
    		
    		// 写数据
    		bos.write("hello".getBytes());
    		
    		// 关闭流
    		bos.close();
    		os.close();
    	}
    }
    

    2.2 字节缓冲输入流BufferedInputStream

    public class BufferedStreamDemo {
    	public static void main(String[] args) throws IOException {
    		// 读操作
    		read();
    	}
    
    	public static void read() throws IOException {
    		// 创建流
    		InputStream is = new FileInputStream("e:/text.txt");
    		BufferedInputStream bis = new BufferedInputStream(is);
    		
    		// 读取数据
    		int ch = -1;
    		while( (ch = bis.read()) != -1 ){
    			System.out.println((char) ch);
    		}
    		
    		// 关闭流
    		bis.close();
    		is.close();
    	}
    }
    

    2.3 不同流复制文件的写法

    public class CopyAVI {
    	/*
    	 * 采用四种方式复制
    	 * 方式一:采用基本的流,一次一个字节的方式复制	no4
    	 * 方式二:采用基本的流,一次多个字节的方式复制	no2
    	 * 方式三:采用高效的流,一次一个字节的方式复制	no3
    	 * 方式四:采用高效的流,一次多个字节的方式复制	no1
    	 */
    	public static void main(String[] args) throws IOException {
    		long start = System.currentTimeMillis();
    		
    		// 方式一:
    		//method1("E:/test.avi");
    		
    		// 方式二:
    		//method2("E:/test.avi");
    		
    		// 方式三:
    		//method3("E:/test.avi");
    		
    		// 方式四:
    		method4("E:/test.avi");
    		
    		long end = System.currentTimeMillis();
    		
    		// 输入计算用时
    		System.out.println(end - start);
    	}
    
    	/*
    	 * 方式四:采用高效的流,一次多个字节的方式复制
    	 */
    	public static void method4(String file) throws IOException {
    		// 创建缓冲输入流
    		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    		
    		// 创建缓冲输出流
    		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:/copy4.avi"));
    		
    		byte[] b = new byte[1024 * 8 * 8];
    		int len;
    		while( (len = bis.read(b)) != -1 ){
    			bos.write(b, 0, len);
    		}
    		
    		// 关闭流
    		bis.close();
    		bos.close();
    	}
    
    	/*
    	 * 方式三:采用高效的流,一次一个字节的方式复制
    	 */
    	private static void method3(String file) throws IOException {
    		// 创建缓冲输入流
    		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    		
    		// 创建缓冲输出流
    		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:/copy2.avi"));
    		
    		int ch = -1;
    		while( (ch = bis.read()) != -1 ){
    			bos.write(ch);
    		}
    		
    		// 关闭流
    		bis.close();
    		bos.close();
    	}
    
    	/*
    	 * 方式二:采用基本的流,一次多个字节的方式复制
    	 */
    	private static void method2(String file) throws IOException {
    		InputStream is = new FileInputStream(file);
    		OutputStream os = new FileOutputStream("e:/copy2.avi");
    		
    		byte[] b = new byte[1024 * 8 * 8];
    		int len;
    		while( (len = is.read(b)) != -1 ){
    			os.write(b, 0, len);
    		}
    		
    		is.close();
    		os.close();
    	}
    
    	/*
    	 * 方式一:采用基本的流,一次一个字节的方式复制
    	 */
    	public static void method1(String file) throws IOException {
    		InputStream is = new FileInputStream(file);
    		OutputStream os = new FileOutputStream("e:/copy1.avi");
    		
    		int ch = -1;
    		while( (ch = is.read()) != -1 ){
    			os.write(ch);
    		}
    		
    		is.close();
    		os.close();
    	}
    }
    

    三、字符缓冲流

    字符输出流:BufferedWriter

    字符输入流:BufferedReader


    3.1 BufferedWriter类

    public class BufferedWriterDemo {
    	public static void main(String[] args) throws IOException {
    		// 创建流
    		Writer writer = new FileWriter("e:/text.txt");
    		BufferedWriter bw = new BufferedWriter(writer);
    		
    		// 写数据
    		for(int i = 0; i < 5; i++){
    			bw.write("hello");
    			bw.newLine();   //写入一个分隔符
    		}
    		
    		// 关闭流
    		bw.close();
    		writer.close();
    	}
    }
    

    3.2 BufferedReader类

    public class BufferedReaderDemo {
    	public static void main(String[] args) throws IOException {
    		// 创建流
    		BufferedReader br = new BufferedReader(new FileReader("e:/text.txt"));
    		
    		// 读取数据
    		//一次一个字符
    		//一次一个字符数组
    		//一次读取文本中一行的字符串内容
    		String line;
    		while( (line = br.readLine()) != null ){
    			System.out.println(line);
    		}
    		
    		// 关闭流
    		br.close();
    	}
    }
    

    3.3 使用缓冲流复制文件

    public class CopyFileByBuffered {
    	public static void main(String[] args) throws IOException {
    		// 创建流
    		BufferedReader br = new BufferedReader(new FileReader("e:/text.txt"));
    		BufferedWriter bw = new BufferedWriter(new FileWriter("e:/text-副本.txt"));
    		
    		// 读取
    		// 复制 —— 写入
    		String line;
    		while( (line = br.readLine()) != null ){
    			bw.write(line);
    			// 换行
    			bw.newLine();
    		}
    		
    		// 关闭流
    		br.close();
    		bw.close();
    	}
    }
    
  • 相关阅读:
    在o(N log N)时间内使用恒定的空间复杂度对链表进行排序
    归并排序
    The method of type must override a superclass method解决方式
    android sdk design Iconography
    android sdk design Typography
    android sdk design Metrics and Grids
    android模拟器被点击时总是自动输入C的解决
    “System.Data.Entity.ModelConfiguration.ModelValidationException”类型的未经处理的异常在 EntityFramework.dll 中发生
    js中的this使用
    MVC身份认证 解决办法 测试可行
  • 原文地址:https://www.cnblogs.com/nadou/p/13974079.html
Copyright © 2011-2022 走看看