使用FileInputStream和FileOutputStream讀寫非文字檔案
阿新 • • 發佈:2022-01-25
/* 實現對圖片的複製操作 */ @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(); } } }