• Java IO输入输出流 FileWriter 字符流


     字节缓冲流

    //为什么要使用包装流,使用包装流是为了提高读写操作的性能。
    public class Packing_flowDemo {
    	public static void main(String[] args) throws Exception {
    		File file = new File("file/packing_flow.txt");
    		//包装流的写法,缓冲区内存大小。1024*8=8192  (byte)
    //		BufferedOutputStream packing = new BufferedOutputStream(new FileOutputStream(file, true));
    //		packing.write("大家好!你好吗?how are your !".getBytes());
    //		packing.close();
         //包装流的读写操作。
    		BufferedInputStream outPacking = new BufferedInputStream(new FileInputStream(file));
    		byte[] buffer = new byte[1024];
    		int len = -1;
    		while ((len = outPacking.read(buffer)) != -1) {
    			System.out.println(new String(buffer, 0, len));
    		}
    	}
    }
    
    public static void main(String[] args) throws IOException {//为了代码看起来美观一些,直接抛出去
    		File file=new File("moves/许嵩 - 素颜 - 现场版.mp3");
    		File file1=new File("moves/许嵩 - 素颜.mp3");
    		//text(file, file1);
    		//text2(file, file1);
    		//text3(file, file1);
    		text4(file, file1);
    	}
    	private static void text(File file,File file1) throws IOException {
    		//节点流的方法,一个一个字节的读和写
    		long begin=System.currentTimeMillis();
    		FileInputStream in=new FileInputStream(file);
    		FileOutputStream out =new FileOutputStream(file1);
    		int len=-1;
    		while((len=in.read())!=-1){
    			out.write(len);
    		}
    		in.close();
    		out.close();
    		System.out.println(System.currentTimeMillis()-begin);//5547毫秒
    	}
    	
    	private static void text2(File file,File file1) throws IOException {
    		//缓冲流的写法,一个一个字节的读和写
    		long begin=System.currentTimeMillis();
    		BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));
    		BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file1));
    		int len=-1;
    		while(in.read()!=-1){
    			out.write(len);
    		}
    		in.close();
    		out.close();
    		System.out.println(System.currentTimeMillis()-begin);//63毫秒
    	}
    	
    	private static void text3(File file,File file1) throws IOException {
    		//节点流的写法,一次性读取1024个字节
    		long begin=System.currentTimeMillis();
    		FileInputStream in=new FileInputStream(file);
    		FileOutputStream out =new FileOutputStream(file1);
    		int len=-1;
    		byte[] buffer=new byte[1024];
    		while((len=in.read(buffer))!=-1){
    			out.write(buffer,0,len);
    		}
    		in.close();
    		out.close();
    		System.out.println(System.currentTimeMillis()-begin);//38毫秒
    	}
    	
    	private static void text4(File file,File file1) throws IOException {
    		//缓冲流的写法,一次性读取1024个字节
    		long begin=System.currentTimeMillis();
    		BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));
    		BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file1));
    		int len=-1;
    		byte[] buffer=new byte[1024];
    		while((len=in.read(buffer))!=-1){
    			out.write(buffer,0,len);
    		}
    		in.close();
    		out.close();
    		System.out.println(System.currentTimeMillis()-begin);//4毫秒
    	}
    
  • 相关阅读:
    Wireshark抓取网站用户名密码
    为ctf而生的web扫描器ctf-wscan
    windows下pip安装python模块时报错总结
    火绒剑获取IP位置
    shodan 搜寻公共摄像头
    肥猪流码农的逆袭之路(1)
    如何备份centos系统
    centos下nginx搭建drupal 8
    openstack学习笔记9-selfservice网络配置
    术语解释-什么是Overlay Network和Underlay Network
  • 原文地址:https://www.cnblogs.com/jiangxifanzhouyudu/p/6721140.html
Copyright © 2020-2023  润新知