zoukankan      html  css  js  c++  java
  • java_24.1文件流的应用--复制文件

    注意:先开的流要最后关

    用字节流传输

    public class Demo {
    	public static void main(String[] args){
    		FileInputStream fis = null;
    		FileOutputStream fos = null;
    		try {
    			fis = new FileInputStream("d:\aaa.txt");
    			fos = new FileOutputStream("d:\bbb.txt");
    			
    			//字节输入流  读取一个字节   写一个字节
    			int len =0;
    			while((len=fis.read())!=-1) {
    				fos.write(len);
    			}
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally {
    			if(fos!=null) {
    				try {
    					fos.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    			if(fis!=null) {
    				try {
    					fis.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    }
    

     用字符数组传输

    public class Demo {
    	public static void main(String[] args){
    		FileInputStream fis = null;
    		FileOutputStream fos = null;
    		try {
    			fis = new FileInputStream("d:\aaa.txt");
    			fos = new FileOutputStream("d:\bbb.txt");
    			
    			//定义字符数组
    			byte[] b = new byte[1024];
    			//读取操作
    			int len = 0;
    			while((len = fis.read(b))!=-1) {
    				fos.write(b,0,len);
    			}
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally {
    			if(fos!=null) {
    				try {
    					fos.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    			if(fis!=null) {
    				try {
    					fis.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    }
    
  • 相关阅读:
    背包问题
    计蒜客lev3
    线段树BIT操作总结
    图论题收集
    Codeforces Round #607 (Div. 2) 训练总结及A-F题解
    2-sat 学习笔记
    洛谷 P3338 【ZJOI2014】力/BZOJ 3527 力 题解
    $noi.ac$ #51 array 题解
    洛谷 P3292 【SCOI2016】幸运数字/BZOJ 4568 幸运数字 题解
    洛谷 P5283 【十二省联考2019】异或粽子 题解
  • 原文地址:https://www.cnblogs.com/smxbo/p/10698458.html
Copyright © 2011-2022 走看看