字节流 读写的操作

时间:2022-3-14    作者:老大夫    分类: Java作业


如果需要复制网络地址的文件时,需要将url转字符串变成路径.

package CopyTest;

import java.io.*;
import java.net.URL;

public class CCopyTest {
    public static void main(String[] args) throws IOException {
        InputStream is=null;
        OutputStream os=null;
        try{
        //可以复制网络地址的文件
//            URL url = new URL("1.png");
//            is = new BufferedInputStream(url.openStream());
            is=new FileInputStream("1.png");
            os=new FileOutputStream("2.png");

            byte[] bytes=new byte[1024];
            int len=0;
            while((len=is.read(bytes))!=-1){
                os.write(bytes,0,len);
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(os !=null){
                try{
                    os.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(is !=null){
                try{
                    is.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }

    }
}

版权所有:伸手党盘
文章标题:字节流 读写的操作
文章链接:https://ssdpan.cn/?post=24
本站文章来源于网络搜集和原创,仅供学习使用,未经授权请勿用于任何商业用途,如有侵权及时联系删除

推荐阅读:


扫描二维码,在手机上阅读