溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java _io_文件的拷貝,面向?qū)ο箫L(fēng)格

發(fā)布時(shí)間:2020-07-12 09:20:07 來源:網(wǎng)絡(luò) 閱讀:213 作者:wx5d21d5e6e5ab1 欄目:編程語(yǔ)言

//文件拷貝,以程序?yàn)橹修D(zhuǎn)站,從一個(gè)文件到另一個(gè)文件
可處理圖片和文本
思路:
type[] flush = new type[1024]
在 .read(flush) ,時(shí)已將將內(nèi)容存儲(chǔ)到字節(jié)數(shù)組,只需再進(jìn)行寫出即可
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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI