1. 程式人生 > 其它 >使用FileInputStream和FileOutputStream讀寫非文字檔案

使用FileInputStream和FileOutputStream讀寫非文字檔案

/*
    實現對圖片的複製操作
     */
    @Test
    public void testFIleInputOutputStream(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //
            File srcFile = new File("愛情與友情.jpg");
            File destFile = new File("愛情與友情2.jpg");

            //
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);

            //複製的過程
            byte[] buffer = new byte[5];
            int len;
            while ((len = fis.read(buffer)) != -1){
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }