zoukankan      html  css  js  c++  java
  • 关于读写任意文件类型的方法

    package test;
    
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.LineNumberReader;
    import java.math.BigDecimal;
    import java.nio.channels.NonReadableChannelException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    import privateclass.MyBufferedReader;
    
    public class Main {
    
    	private static final String space_operator = " ";
    	private static final double pi = Math.PI;
    	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    	public static void main(String[] args) throws Exception {
    
    		demo_1();
    		demo_2();
    		demo_3();
    		demo_4();		
    	}
    	private static void demo_1() throws Exception {
    		FileInputStream fis = new FileInputStream("d:\daxiao.mp4");
    		FileOutputStream fos = new FileOutputStream("d:\Copy_daxiao1.mp4");
    		
    		byte [] buf = new byte[1024];
    		int len = 0;
    		while((len = fis.read(buf)) != -1)
    		{
    			fos.write(buf,0,len);
    		}
    		fis.close();
    		fos.close();
    	}
    	
    	/*
    	 * 这个方法是一个字节一个字节的读取
    	 * 很慢不建议使用
    	 */
    	private static void demo_2() throws Exception {
    		FileInputStream fis = new FileInputStream("d:\daxiao.mp4");
    		FileOutputStream fos = new FileOutputStream("d:\Copy_daxiao2.mp4");
    	
    		int len = 0;
    		while((len = fis.read()) != -1)
    		{
    			fos.write(len);
    		}
    		fis.close();
    		fos.close();
    		
    	}
    	private static void demo_3() throws Exception {
    		
    		FileInputStream fis = new FileInputStream("d:\daxiao.mp4");
    		FileOutputStream fos = new FileOutputStream("d:\Copy_daxiao3.mp4");
    		
    		BufferedInputStream bis = new BufferedInputStream(fis);
    		BufferedOutputStream bos = new BufferedOutputStream(fos);
    		
    		int len = 0;
    		while((len = bis.read()) != -1)
    		{
    			bos.write(len);
    		}
    		bis.close();
    		bos.close();
    	}
    	
    	/*
    	 * 缓冲区的大小就是我们所读文件的大小
    	 * 但是当文件太大的时候我们就要考虑爆数组的问题了
    	 */
    	private static void demo_4() throws Exception {
    		
    		FileInputStream fis = new FileInputStream("d:\daxiao.mp4");
    		FileOutputStream fos = new FileOutputStream("d:\Copy_daxiao4.mp4");
    		
    		byte [] buf = new byte[fis.available()];
    	
    		fis.read(buf);
    		fos.write(buf);
    		
    		fis.close();
    		fos.close();
    	}
    }
    

      

  • 相关阅读:
    Sharepoint2013搜索学习笔记之创建搜索服务(二)
    Sharepoint2013搜索学习笔记之设置外网内容源(四)
    C# zxing插件 根据输入的字符串生成二维码
    值类型和引用类型
    Web.config增删查改
    Redis
    2017年最好的6个WEB前端开发手册下载
    php implode()函数详解
    elk日志系统搭建
    aop实现接口请求参数打印
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/11788803.html
Copyright © 2011-2022 走看看