java的文件流:字節流(FileInputStream、FileOutputStream)和字符流(FileReader、FileWriter)。


    java的輸入輸出建立在4個抽象類的基礎上:InputStream、OutputStream、Reader、Writer。InputSream和OutputStream被設計成字節流類,而Reader和Writer被設計成字符流類。一般來說,處理字符或者字符串時應該使用字符流類,處理字節或者二進制對象時應該使用字節流類。

  一般在操作文件流時,不管是字節流還是字符流,都可以按照以下的方式進行。

1、使用File類找到一個文件

2、通過File類實例化字節流或字符流

3、進行字節(字符)的讀寫操作

4、關閉文件流

下面是2個例子,向文件中寫入字符串並讀出。

以字節流方式:

 1 package zdxtest;
 2 import java.io.*;
 3 public class TestFile1 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         //字節流輸入輸出,向一個文件寫入內容然后讀出
 8         File f = new File("d:\\1.txt");
 9         FileOutputStream fos = null;
10         FileInputStream foi = null;
11         String sin = "今天是七月最后一天";
12         //創建文件,如果目錄下存在文件,先刪除再創建
13         if (f.exists()) {
14             f.delete();
15             try {
16                 f.createNewFile();
17             } catch (IOException e) {
18                 // TODO Auto-generated catch block
19                 e.printStackTrace();
20             }    
21         }
22         else{
23             try {
24                 f.createNewFile();
25             } catch (IOException e) {
26                 // TODO Auto-generated catch block
27                 e.printStackTrace();
28             }
29         }
30         //實例化字節流子類:FileOutputStream
31         try {
32             fos = new FileOutputStream(f);
33         } catch (FileNotFoundException e) {
34             // TODO Auto-generated catch block
35             e.printStackTrace();
36         }
37         //以字節的方式寫入文件並關閉流
38         byte[] b = sin.getBytes();
39         try {
40             fos.write(b);
41             fos.close();
42         } catch (IOException e) {
43             // TODO Auto-generated catch block
44             e.printStackTrace();
45         }
46         //實例化字節流子類:FileInputStream
47         try {
48             foi = new FileInputStream(f);
49         } catch (FileNotFoundException e) {
50             // TODO Auto-generated catch block
51             e.printStackTrace();
52         }
53         //以字節的方式讀取文件並關閉流
54         byte[] c = new byte[1000];
55         int  l=0;
56         try {        
57             /*l = foi.read();
58             while(l!=-1){
59                 System.out.println(String.valueOf(l));
60                 l=foi.read();
61             }*/
62             l = foi.read(c);
63             foi.close();
64         } catch (IOException e) {
65             // TODO Auto-generated catch block
66             e.printStackTrace();
67         }
68         String o = new String(c);
69         System.out.println(o); //打印文件內容
70         System.out.println(l); //打印讀取的字節數
71 
72     }
73 
74 }

運行后輸出:

今天是七月最后一天

27

為什么是27個字節呢,因為我eclipse的默認編碼方式是UTF-8,在UTF-8編碼方式中,一個漢字是占有三個字節的,一共9個漢字,所以是27個字節。

再來看以字符流的方式實現:

 1 package zdxtest;
 2 import java.io.*;
 3 
 4 public class FileTest2 {
 5 
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         File f = new File("d:\\2.txt");
 9         FileReader fr = null;
10         FileWriter fw = null;
11         String s = "明天是八月第一天";
12         //創建文件,如果目錄下存在文件,先刪除再創建
13         if(f.exists())
14         {
15             f.delete();
16             try {
17                 f.createNewFile();
18             } catch (IOException e) {
19                 // TODO Auto-generated catch block
20                 e.printStackTrace();
21             }
22         }
23         else{
24             try {
25                 f.createNewFile();
26             } catch (IOException e) {
27                 // TODO Auto-generated catch block
28                 e.printStackTrace();
29             }
30         }
31         //實例化字符流子類:FileWriter,以字符的方式寫入文件並關閉流
32         try {
33             fw = new FileWriter(f);
34             fw.write(s);
35             fw.close();
36         } catch (IOException e) {
37             // TODO Auto-generated catch block
38             e.printStackTrace();
39         }
40         //實例化字符流子類:FileReader,以字符的方式讀取文件並關閉流
41         char [] b = new char[1000];
42         int l = 0;
43         try {
44             fr = new FileReader(f);
45             l = fr.read(b);
46         } catch (IOException e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         }
50         System.out.println(new String(b));  //打印文件內容
51         System.out.println(l);    //打印讀取的字符數
52     }
53 
54 }

運行輸出:

明天是八月第一天

8

一共8個漢字,一個char存儲一個漢字,所以是8個字符

 


免責聲明!

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



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