Java之數據流-復制二進制文件


 

 1 package test_demo.fileoper;
 2 
 3 import java.io.*;
 4 
 5 /*
 6 * 數值字節流操作,復制二進制文件
 7 * 輸入流:從文件中讀取數據,擴展為數據流(二進制)
 8 * 輸出流:將數據流數據輸出到文件中
 9 * 注意關閉輸入輸出流
10 * */
11 public class DataInOutSteamOper {
12     public static void main(String args[]) {
13         FileInputStream fis = null; //輸入流
14         FileOutputStream fos = null;    //輸出流
15         //數據流(二進制)
16         DataInputStream dis = null;
17         DataOutputStream dos = null;
18 
19         try {
20             fis = new FileInputStream("C:\\testdata\\filedir\\a.jpg");
21             fos = new FileOutputStream("C:\\testdata\\filedir\\b.jpg");
22             dis = new DataInputStream(fis);
23             dos = new DataOutputStream(fos);
24             int i = 0;  //存放讀取的byte數組的長度
25             //通過輸入流讀取數據,byte[]有默認值
26             while ((i = dis.read()) != -1) {
27                 dos.write(i);
28             }
29             System.out.println("文件復制成功!");
30         } catch (IOException e) {
31             e.printStackTrace();
32         } finally {
33             try {
34                 //關閉輸入輸出流
35                 dos.close();
36                 fos.close();
37                 dis.close();
38                 fis.close();
39             } catch (IOException e) {
40                 e.printStackTrace();
41             }
42         }
43     }
44 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM