1. 程式人生 > >Java IO-2 IO流

Java IO-2 IO流

頂級 read zip class 安裝 exce 字符數 write demo

1.字節流介紹

InputStream和OutputStream是字節流的頂級父類, 所有的字節輸入流繼承自InputStream, 所有的字節輸出流繼承自OutputStream

2.FileOutputStream

 1 package deom03;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 
 6 public class FileOutputStreamDemo {
 7     public static void main(String[] args) throws IOException {
8 FileOutputStream out = new FileOutputStream("c:\\out.txt"); 9 //void write(int b) 向輸出流寫入一個字節 d 10 out.write(100); 11 out.close(); 12 13 byte[] bytes = {65,66,67,68}; 14 FileOutputStream out1 = new FileOutputStream("c:\\out1.txt"); 15 //
void write(byte[] b) 寫入字節數組 ABCD 16 out1.write(bytes); 17 out1.close(); 18 19 FileOutputStream out2 = new FileOutputStream("c:\\out2.txt"); 20 //向文件中寫入一個字符串 21 out2.write("hello".getBytes()); 22 out2.close(); 23 24 //追加寫入 25 FileOutputStream out3 = new
FileOutputStream("c:\\out3.txt", true); 26 //向文件中寫入一個字符串 27 out3.write("hello".getBytes()); 28 out3.write("world".getBytes()); 29 out3.close(); 30 31 //追加 換行寫入 32 FileOutputStream out4 = new FileOutputStream("c:\\out4.txt", true); 33 //向文件中寫入一個字符串 34 out4.write("hello".getBytes()); 35 out4.write("\r\n".getBytes()); 36 out4.write("world".getBytes()); 37 out4.close(); 38 39 //異常處理 40 MyException(); 41 } 42 43 //異常處理 44 public static void MyException() { 45 FileOutputStream out5 = null; 46 try { 47 out5 = new FileOutputStream("c:\\out5.txt"); 48 out5.write(100); 49 } catch (IOException e) { 50 System.out.println(e.getMessage()); 51 throw new RuntimeException("文件寫入失敗"); 52 } finally { 53 try { 54 if (out5 != null) { 55 out5.close(); 56 } 57 } catch (IOException e2) { 58 System.out.println(e2.getMessage()); 59 throw new RuntimeException("關閉資源失敗"); 60 } 61 62 } 63 } 64 }

3.FileInputStream

 1 package deom03;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.IOException;
 5 
 6 //input1.txt 內容 abc
 7 public class FileInputStreamDemo {
 8     public static void main(String[] args) throws IOException {
 9         FileInputStream in = new FileInputStream("c:\\input1.txt");
10         
11         //int read() 讀取一個字節轉換為整數並返回這一整數
12         int i = 0;
13         while ((i = in.read()) != -1) {
14             System.out.println(i);
15         }
16         in.close();
17         
18         //int read(byte[] b) 讀取若幹字節並保存到字節數組中
19         FileInputStream in1 = new FileInputStream("c:\\input1.txt");
20         byte[] b = new byte[3];
21         int len = in1.read(b);
22         System.out.println(new String(b));    //abc
23         System.out.println(len);            //3
24         in1.close();
25     }
26 }

4.文件復制

第一種方式

 1 package deom03;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 //文件復制-效率低
 8 public class Copy {
 9     public static void main(String[] args) {
10         FileInputStream in = null;
11         FileOutputStream out = null;
12         try {
13             in = new FileInputStream("c:\\xampp安裝包.zip");
14             out = new FileOutputStream("c:\\11.zip");
15             
16             int len = 0;
17             while ((len = in.read()) != -1) {
18                 out.write(len);
19             }
20         } catch (IOException e) {
21             System.out.println(e);
22             System.out.println("文件復制失敗");
23         } finally {
24             try {
25                 if (out != null) {
26                     out.close();
27                 }
28             } catch (IOException e1) {
29                 throw new RuntimeException("釋放資源失敗");
30             } finally {
31                 try {
32                     if (in != null) {
33                         in.close();
34                     }
35                 } catch (IOException e2) {
36                     throw new RuntimeException("釋放資源失敗");
37                 }
38             }
39         }
40     }
41 }

第二種方式

 1 package deom03;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 //文件復制-效率高
 8 public class CopyFast {
 9     public static void main(String[] args) {
10         FileInputStream in = null;
11         FileOutputStream out = null;
12         
13         try {
14             in = new FileInputStream("c:\\xampp安裝包.zip");
15             out = new FileOutputStream("c:\\11.zip");
16             byte[] bytes = new byte[1024];
17             int len = 0;
18             while ((len = in.read(bytes)) != -1) {
19                 out.write(bytes, 0, len);
20             }
21         } catch (IOException e) {
22             System.out.println(e);
23             System.out.println("文件復制失敗");
24         } finally {
25             try {
26                 if (out != null) {
27                     out.close();
28                 }
29             } catch (IOException e1) {
30                 throw new RuntimeException("釋放資源失敗");
31             } finally {
32                 try {
33                     if (in != null) {
34                         in.close();
35                     }
36                 } catch (IOException e2) {
37                     throw new RuntimeException("釋放資源失敗");
38                 }
39             }
40         }
41     }
42 }

5.字符流介紹

Reader和Writer是字符流的頂級父類, 所有的字符輸入流繼承自Reader, 所有的字符輸出流繼承自Writer

6.FileReader

 1 package deom03;
 2 
 3 import java.io.FileReader;
 4 import java.io.IOException;
 5 
 6 public class ReaderDemo {
 7     public static void main(String[] args) throws IOException {
 8         FileReader fr = new FileReader("c:\\1.txt");
 9         int len = 0;
10         while ((len = fr.read()) != -1) {
11             System.out.print((char)len);
12         }
13         fr.close();
14         
15         FileReader fr1 = new FileReader("c:\\1.txt");
16         char[] ch = new char[1024];
17         int len1 = 0;
18         while ((len1 = fr1.read(ch)) != -1) {
19             System.out.println(new String(ch, 0, len1));
20         }
21     }
22 }

7.FileWriter

 1 package deom03;
 2 
 3 import java.io.FileWriter;
 4 import java.io.IOException;
 5 
 6 public class WriterDemo {
 7     public static void main(String[] args) throws IOException {
 8         FileWriter fw = new FileWriter("c:\\1.txt");
 9         
10         //寫一個字符
11         fw.write(100);
12         fw.flush();
13         
14         //寫一個字符數組
15         char[] c = {‘a‘,‘b‘,‘c‘};
16         fw.write(c);
17         fw.flush();
18         
19         //寫一個字符數組一部分
20         fw.write(c,0,2);
21         fw.flush();
22         
23         //寫一個字符串
24         fw.write("你好");
25         fw.flush();
26         
27         fw.close();
28     }
29 }

Java IO-2 IO流