JAVA學習--文件流FileInputStream和FileOutputStream操作


 * 1.流的分類:
 * 按照數據流向的不同:輸入流  輸出流
 * 按照處理數據的單位的不同:字節流  字符流(處理的文本文件)
 * 按照角色的不同:節點流(直接作用於文件的)  處理流
 *
 * 2.IO的體系
 * 抽象基類            節點流(文件流)                                緩沖流(處理流的一種)
 * InputStream        FileInputStream            BufferedInputStream
 * OutputStream        FileOutputStream        BufferedOutputStream
 * Reader            FileReader                BufferedReader
 * Writer            FileWriter                BufferedWriter

 

 1     // 從硬盤存在的一個文件中,讀取其內容到程序中。使用FileInputStream
 2     // 要讀取的文件一定要存在。否則拋FileNotFoundException
 3     @Test
 4     public void testFileInputStream1() throws Exception {
 5         // 1.創建一個File類的對象。
 6         File file = new File("hello.txt");
 7         // 2.創建一個FileInputStream類的對象
 8         FileInputStream fis = new FileInputStream(file);
 9         // 3.調用FileInputStream的方法,實現file文件的讀取。
10         /*
11          * read():讀取文件的一個字節。當執行到文件結尾時,返回-1
12          */
13         // int b = fis.read();
14         // while(b != -1){
15         // System.out.print((char)b);
16         // b = fis.read();
17         // }
18         int b;
19         while ((b = fis.read()) != -1) {
20             System.out.print((char) b);
21         }
22         // 4.關閉相應的流
23         fis.close();
24     }
 1     // 使用try-catch的方式處理如下的異常更合理:保證流的關閉操作一定可以執行
 2     @Test
 3     public void testFileInputStream2() {
 4         // 2.創建一個FileInputStream類的對象
 5         FileInputStream fis = null;
 6         try {
 7             // 1.創建一個File類的對象。
 8             File file = new File("hello.txt");
 9             fis = new FileInputStream(file);
10             // 3.調用FileInputStream的方法,實現file文件的讀取。
11             int b;
12             while ((b = fis.read()) != -1) {
13                 System.out.print((char) b);
14             }
15         } catch (IOException e) {
16             e.printStackTrace();
17         } finally {
18             // 4.關閉相應的流
19             if (fis != null) {
20                 try {
21                     fis.close();
22                 } catch (IOException e) {
23                     // TODO Auto-generated catch block
24                     e.printStackTrace();
25                 }
26             }
27         }
28     }
 1     @Test
 2     public void testFileInputStream3() { // abcdefgcde
 3         FileInputStream fis = null;
 4         try {
 5             File file = new File("hello.txt");
 6             fis = new FileInputStream(file);
 7             byte[] b = new byte[5];// 讀取到的數據要寫入的數組。
 8             int len;// 每次讀入到byte中的字節的長度
 9             while ((len = fis.read(b)) != -1) {
10                 // for (int i = 0; i < len; i++) {
11                 // System.out.print((char) b[i]);
12                 // }
13                 String str = new String(b, 0, len);
14                 System.out.print(str);
15             }
16         } catch (IOException e) {
17             e.printStackTrace();
18         } finally {
19             if (fis != null) {
20                 try {
21                     fis.close();
22                 } catch (IOException e) {
23                     // TODO Auto-generated catch block
24                     e.printStackTrace();
25                 }
26 
27             }
28 
29         }
30     }
 1     // FileOutputStream
 2     @Test
 3     public void testFileOutputStream() {
 4         // 1.創建一個File對象,表明要寫入的文件位置。
 5         // 輸出的物理文件可以不存在,當執行過程中,若不存在,會自動的創建。若存在,會將原有的文件覆蓋
 6         File file = new File("hello2.txt");
 7         // 2.創建一個FileOutputStream的對象,將file的對象作為形參傳遞給FileOutputStream的構造器中
 8         FileOutputStream fos = null;
 9         try {
10             fos = new FileOutputStream(file);
11             // 3.寫入的操作
12             fos.write(new String("I love China!").getBytes());
13         } catch (Exception e) {
14             e.printStackTrace();
15         } finally {
16             // 4.關閉輸出流
17             if (fos != null) {
18                 try {
19                     fos.close();
20                 } catch (IOException e) {
21                     // TODO Auto-generated catch block
22                     e.printStackTrace();
23                 }
24             }
25         }
26     }
 1     // 從硬盤讀取一個文件,並寫入到另一個位置。(相當於文件的復制)
 2     @Test
 3     public void testFileInputOutputStream() {
 4         // 1.提供讀入、寫出的文件
 5         File file1 = new File("C:\\Users\\shkstart\\Desktop\\1.jpg");
 6         File file2 = new File("C:\\Users\\shkstart\\Desktop\\2.jpg");
 7         // 2.提供相應的流
 8         FileInputStream fis = null;
 9         FileOutputStream fos = null;
10         try {
11             fis = new FileInputStream(file1);
12             fos = new FileOutputStream(file2);
13             // 3.實現文件的復制
14             byte[] b = new byte[20];
15             int len;
16             while ((len = fis.read(b)) != -1) {
17                 // fos.write(b);//錯誤的寫法兩種: fos.write(b,0,b.length);
18                 fos.write(b, 0, len);
19             }
20         } catch (Exception e) {
21             e.printStackTrace();
22         } finally {
23             if (fos != null) {
24                 try {
25                     fos.close();
26                 } catch (IOException e) {
27                     e.printStackTrace();
28                 }
29             }
30             if (fis != null) {
31                 try {
32                     fis.close();
33                 } catch (IOException e) {
34                     e.printStackTrace();
35                 }
36             }
37 
38         }
39     }
 1     // 實現文件復制的方法
 2     public void copyFile(String src, String dest) {
 3         // 1.提供讀入、寫出的文件
 4         File file1 = new File(src);
 5         File file2 = new File(dest);
 6         // 2.提供相應的流
 7         FileInputStream fis = null;
 8         FileOutputStream fos = null;
 9         try {
10             fis = new FileInputStream(file1);
11             fos = new FileOutputStream(file2);
12             // 3.實現文件的復制
13             byte[] b = new byte[1024];
14             int len;
15             while ((len = fis.read(b)) != -1) {
16                 // fos.write(b);//錯誤的寫法兩種: fos.write(b,0,b.length);
17                 fos.write(b, 0, len);
18             }
19         } catch (Exception e) {
20             e.printStackTrace();
21         } finally {
22             if (fos != null) {
23                 try {
24                     fos.close();
25                 } catch (IOException e) {
26                     e.printStackTrace();
27                 }
28             }
29             if (fis != null) {
30                 try {
31                     fis.close();
32                 } catch (IOException e) {
33                     e.printStackTrace();
34                 }
35             }
36 
37         }
38     }
 1     @Test
 2     public void testCopyFile(){
 3         long start = System.currentTimeMillis();
 4 //        String src = "C:\\Users\\shkstart\\Desktop\\1.avi";
 5 //        String dest = "C:\\Users\\shkstart\\Desktop\\2.avi";
 6         String src = "dbcp.txt";
 7         String dest = "dbcp2.txt";
 8         copyFile(src,dest);
 9         long end = System.currentTimeMillis();
10         System.out.println("花費的時間為:" + (end - start));//3198
11     }

 


免責聲明!

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



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