码迷,mamicode.com
首页 > 其他好文 > 详细

文件复制流

时间:2016-08-06 22:00:46      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:java

1,示例代码

    

package com.dsczs.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 读文件和写文件操作 还可以进行图片的复制操作 带缓存流
 * @author Administrator
 *
 */
public class Demo06 {

    public static void main(String[] args){
        InputStream in = null;
        OutputStream out = null;
        try {
            File file = new File("C:/Users/Administrator/Desktop/1/1.png");
            in = new BufferedInputStream(new FileInputStream(file));
            out = new BufferedOutputStream(new FileOutputStream(new File("C:/Users/Administrator/Desktop/1/10.png")));
            byte[] b = new byte[1024];
            int len = 0;
            while(-1 != (len = in.read(b))){
                out.write(b, 0, len);
                out.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}


文件复制流

标签:java

原文地址:http://dsczs.blog.51cto.com/9480367/1835121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!