加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 01:56:52 所属栏目:Java 来源:网络整理
导读://文件拷贝,以程序为中转站,从一个文件到另一个文件 可处理图片和文本 思路: type[] flush = new type[1024] 在 .read(flush) ,时已将将内容存储到字节数组,只需再进行写出即可 os.write(flush,len)然后刷新缓存,os.flush() public class test{ private
//文件拷贝,以程序为中转站,从一个文件到另一个文件
可处理图片和文本
思路:
type[] flush = new type[1024]
在 .read(flush) ,时已将将内容存储到字节数组,只需再进行写出即可
os.write(flush,len)然后刷新缓存,os.flush()

public class test{
        private String path;
        private String path2;
        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.path2=s2;
            first=new File(this.path);
            last=new File(this.path2);
            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,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();
            }
        }

}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读