io补充

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtiles{
    
    public static void upload(File file,String path) throws Exception{
        InputStream is = new FileInputStream(file);
        upload(is,path);
    }
    
    public static void upload(InputStream is,String path) throws Exception{
        OutputStream os = null ;        
        try{
            os = new FileOutputStream(path);
            
            int length = -1;
            byte[] buffer = new byte[7092];
            //从输入流中读取一定数量的字节,并将其存储在缓冲区数组buffer中。
            //以整数形式返回实际读取的字节数.
            //在输入数据可用、检测到文件末尾或者抛出异常前,此方法一直阻塞。
            //如果buffer的长度为 0,则不读取任何字节并返回 0;
            //否则,尝试读取至少一个字节.
            //如果因为流位于文件末尾而没有可用的字节,则返回值-1;
            //否则,至少读取一个字节并将其存储在buffer中。
            //将读取的第一个字节存储在元素 buffer[0]中,下一个存储在buffer[1]中,依次类推.
            //读取的字节数最多等于buffer的长度。设length为实际读取的字节数;
            //这些字节将存储在buffer[0]到buffer[length-1]的元素中,
            //不影响length[k]到length[b.length-1]的元素.
            while(-1!=(length = is.read(buffer,0,7092))){
                os.write(buffer,0,length);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(null != is){
                is.close();
            }
            if(null != os){
                os.close();
            }
        }
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漫路求索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值