java_io_文件的拷贝,面向对象风格-创新互联

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

创新互联主营龙子湖网站建设的网络公司,主营网站建设方案,重庆App定制开发,龙子湖h5成都微信小程序搭建,龙子湖网站营销推广欢迎龙子湖等地区企业咨询
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();
            }
        }

}

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前标题:java_io_文件的拷贝,面向对象风格-创新互联
转载源于:http://ybzwz.com/article/hosdc.html