温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java _io_文件的拷贝,面向对象风格

发布时间:2020-07-12 09:20:07 来源:网络 阅读:205 作者:wx5d21d5e6e5ab1 栏目:编程语言

//文件拷贝,以程序为中转站,从一个文件到另一个文件
可处理图片和文本
思路:
type[] flush = new type[1024]
在 .read(flush) ,时已将将内容存储到字节数组,只需再进行写出即可
os.write(flush,0,len)然后刷新缓存,os.flush()

    public class test{
        private String path;
        private String path3;
        private int len;
        private File first;
        private File last;
        private InputStream is;
        private OutputStream os;
        public static void main(String[]args) 
        {
            test t=new test("C:/Users/10853/eclipse-workspace/hell/src/hell/abc","D:/d/last");
            t.copy();
        } 
        public test(String s,String s2) 
        {
            this.path=s;
            this.path3=s2;
            first=new File(this.path);
            last=new File(this.path3);
            try{
                is=new FileInputStream(first);
                os=new FileOutputStream(last);
            }catch(FileNotFoundException e)
            {
                e.printStackTrace();
            }

    }
    public  void copy()
    {
            //分段读取
        **byte[] flush =new byte[1024];**
            **try {**
                **while((len=is.read(flush))!=-1)**
            **  {**
                    **os.write(flush,0,len);    **
                    **os.flush();**  //写出后要刷新
                **}***
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
            try {
                if(os!=null)
                {
                    os.close();
                }
                if(is!=null)
                {
                    is.close();
                }
            }catch(IOException e)
            {
                e.printStackTrace();
            }
        }

}

}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI