Javaの_io_コピーファイル、オブジェクト指向スタイル

//は、一つのファイルから別のものに、通過点としてプログラムにファイルをコピーします

思考:
[] =新しい新しいタイプのフラッシュ[1024]入力
のみに書き込み、コンテンツはバイト配列に格納されている場合、(フラッシュ).readで
次に更新(フラッシュ、0、LEN)os.writeキャッシュ、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,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();
            }
        }

}

}

おすすめ

転載: blog.51cto.com/14437184/2423153