zoukankan      html  css  js  c++  java
  • java之 ------ 文件拷贝

    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class FileCopyStream {
    
    	public static void main(String[] args) {
    		//fileCopy0("b.dat","d:/ex/a/","d:/ex/b/");
    		//fileCopy1("b.dat","d:/ex/a/","d:/ex/b/");
    		//fileCopy2("b.dat","d:/ex/a/","d:/ex/b/");
    		fileCopy2("1.mp3","d:/ex/a/","d:/ex/b/");
    		//fileCopy3("c.mp3","d:/ex/a/","d:/ex/b/");
    		//fileCopy3("d.txt","d:/ex/a/","d:/ex/b/");
    
    	}
    	private static void fileCopy0(String fileName, String dir1,String dir2){
    		try {
    			FileInputStream in = new FileInputStream(dir1+fileName);
    			FileOutputStream out = new FileOutputStream(dir2+fileName);
    			byte[] buffer = new byte[512];
    			in.read(buffer);
    			out.write(buffer);
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (Exception e) {
    		}
    	}
    	
    	//学习关流
    	private static void fileCopy1(String fileName, String dir1,String dir2){
    		FileInputStream in = null;
    		FileOutputStream out = null;
    		try {
    			in = new FileInputStream(dir1+fileName);
    			out = new FileOutputStream(dir2+fileName);
    			byte[] buffer = new byte[512];
    			in.read(buffer);
    			out.write(buffer);
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (Exception e) {
    		}finally{
    			try {
    				in.close();
    				out.close();
    			} catch (IOException e) {
    				throw new RuntimeException("文件无法关闭");
    			}
    		}
    	}
    	//可以拷贝大文件
    	private static void fileCopy2(String fileName, String dir1,String dir2){
    		FileInputStream in = null;
    		FileOutputStream out = null;
    		try {
    			in = new FileInputStream(dir1+fileName);
    			out = new FileOutputStream(dir2+fileName);
    			byte[] buffer = new byte[512];
    			int num = 0;
    			do{
    				num = in.read(buffer);
    				out.write(buffer,0,num);
    			}while(num>=0);
    			
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally{
    			try {
    				in.close();
    				out.close();
    			} catch (IOException e) {
    				throw new RuntimeException("文件无法关闭");
    			}
    		}
    	}
    	
    	//可以拷贝大文件
    	private static void fileCopy3(String fileName, String dir1,String dir2){
    		FileInputStream in = null;
    		FileOutputStream out = null;
    		try {
    			in = new FileInputStream(dir1+fileName);
    			out = new FileOutputStream(dir2+fileName);
    			byte[] buffer = new byte[512];
    			int num=0;
    			while(in.available()>0){
    				num = in.read(buffer);
    				
    				//最简单的加密
    				for(int i=0;i<num;i++){
    					buffer[i] = (byte)(buffer[i]+1);
    				}
    				
    				out.write(buffer,0,num);
    			}
    			
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (Exception e) {
    		}finally{
    			try {
    				in.close();
    				out.close();
    			} catch (IOException e) {
    				throw new RuntimeException("文件无法关闭");
    			}
    		}
    	}
    
    }
    

  • 相关阅读:
    oracle学习 五 使用存储过程创建一个重置密码为123456的功能(持续更新中)
    oracle学习 四(持续更新中)无法为表空间 MAXDATA 中的段创建 INITIAL 区
    P3224 [HNOI2012]永无乡
    P3521 [POI2011]ROT-Tree Rotations
    UVA11090 Going in Cycle!!
    P1136 迎接仪式
    P1984 [SDOI2008]烧水问题(具体证明)
    P1494 [国家集训队]小Z的袜子
    P2680 运输计划
    P2831 愤怒的小鸟
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5395813.html
Copyright © 2011-2022 走看看