File Reader 12 1 12 1ch 1201 java

  • Slides: 78
Download presentation

字元串流的檔案處理 File. Reader類別 【實用範例12 -1】:開啟純文字檔,並使用字元串流方式讀取 文字檔內容後輸出並統計所讀取的字元個數。 範例12 -1:ch 12_01. java(隨書光碟 my. Javach 12_01. java)

字元串流的檔案處理 File. Reader類別 【實用範例12 -1】:開啟純文字檔,並使用字元串流方式讀取 文字檔內容後輸出並統計所讀取的字元個數。 範例12 -1:ch 12_01. java(隨書光碟 my. Javach 12_01. java) 1 2 3 4 5 6 7 8 9 10 11 12 /* 檔名: ch 12_01. java 功能: 以字元串流讀取文字檔 */ package my. Java. ch 12; import java. lang. *; import java. io. *; public class ch 12_01 //主類別 { public static void main(String args[]) throws IOException, File. Not. Found. Exception {

字元串流的檔案處理 File. Reader類別 13 14 15 16 17 18 19 20 21 22 23

字元串流的檔案處理 File. Reader類別 13 14 15 16 17 18 19 20 21 22 23 char cbuf[] = new char[256]; File. Reader fr = new File. Reader("c: \my. Java\ch 12\file\text 1. txt"); int num = fr. read(cbuf); //讀取最多 256個字元到cbuf陣列中 String str 1 = new String(cbuf, 0, num); //字元陣列轉換為字串 System. out. println("總共讀取" + num + "個字元數"); System. out. println("檔案內容如下"); System. out. println(str 1); fr. close(); //關檔 text 1. text(隨書光碟 my. Javach 12filetext 1. txt) } }

字元串流的檔案處理 File. Reader類別 1 2 3 4 5 6 7 8 9 10 11

字元串流的檔案處理 File. Reader類別 1 2 3 4 5 6 7 8 9 10 11 12 13 14 【實用範例12 -2】:使用迴圈讀取純文字檔的所有內容。 範例12 -2:ch 12_02. java(隨書光碟 my. Javach 12_02. java) /* 檔名: ch 12_02. java 功能: 使用迴圈讀取文字檔的全部內容 */ package my. Java. ch 12; import java. lang. *; import java. io. *; public class ch 12_02 //主類別 { public static void main(String args[]) throws IOException, File. Not. Found. Exception { char cbuf[] = new char[16]; File. Reader fr = new File. Reader("c: \my. Java\ch 12\file\text 1. txt");

字元串流的檔案處理 File. Reader類別 15 16 17 18 19 20 21 22 23 24 25

字元串流的檔案處理 File. Reader類別 15 16 17 18 19 20 21 22 23 24 25 26 int num; String str 1; while((num = fr. read(cbuf)) !=-1) //使用迴圈讀取文字檔的全部內容 { str 1 = new String(cbuf, 0, num); //字元陣列轉換為字串 System. out. println("總共讀取" + num + "字元數"); System. out. println(str 1); System. out. println("------------"); } fr. close(); //關檔 } }

字元串流的檔案處理 File. Writer類別 相對於File. Reader的讀取功能,File. Writer類別則提供寫入文 字檔的功能。同樣地,Output. Stream. Writer為Writer的子類別, File. Writer為Output. Stream. Writer的子類別(見圖 12

字元串流的檔案處理 File. Writer類別 相對於File. Reader的讀取功能,File. Writer類別則提供寫入文 字檔的功能。同樣地,Output. Stream. Writer為Writer的子類別, File. Writer為Output. Stream. Writer的子類別(見圖 12 -2),因 此,File. Writer產生的物件也可以使用祖先類別的方法。 寫入的開檔動作同樣包含在建構子,File. Writer的建構子如下: File. Writer的建構子 File. Writer(File file) File. Writer(File file, boolean append) File. Writer(File. Descriptor fd) File. Writer(String file. Name, boolean append) //上述建構子會拋出IOException例外

字元串流的檔案處理 File. Writer類別 1 2 3 4 5 6 7 8 9 10 11

字元串流的檔案處理 File. Writer類別 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 /* 檔名: ch 12_03. java 功能: 以字元串流寫入文字檔 */ package my. Java. ch 12; import java. lang. *; import java. io. *; public class ch 12_03 //主類別 { public static void main(String args[]) throws IOException { String str 1 = "費氏數列如下: "; char end. Ch[] = {'C', 'o', 'n', 't', 'i', 'n', 'u', 'e', '. '}; int num. F; File. Writer fw = new File. Writer("c: \my. Java\ch 12\file\text 2. txt"); fw. write(str 1); fw. write('r'); fw. write('n'); for(int i=1; i<10; i++) { //寫入換行字元

字元串流的檔案處理 File. Writer類別 23 num. F=Fib(i); 24 fw. write(num. F+" "); //int與字串連結, 會自動轉型為字串 25

字元串流的檔案處理 File. Writer類別 23 num. F=Fib(i); 24 fw. write(num. F+" "); //int與字串連結, 會自動轉型為字串 25 } 26 27 fw. write(end. Ch); 28 fw. close(); //關檔 29 } 30 public static int Fib(int n) 31 { 32 if((n==1) || (n==0)) 33 return n; else 34 35 return Fib(n-1)+Fib(n-2); 程式執行完畢,會在 36 } 37  c: my. Javach 12file目錄 } 執行結果  下產生text 2. txt檔,如圖

字元串流的檔案處理 File. Writer類別 【實用範例12 -4】:在原有文字檔案後面增加資料。 範例12 -4:ch 12_04. java(隨書光碟 my. Javach 12_04. java) 1

字元串流的檔案處理 File. Writer類別 【實用範例12 -4】:在原有文字檔案後面增加資料。 範例12 -4:ch 12_04. java(隨書光碟 my. Javach 12_04. java) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 /* 檔名: ch 12_04. java 功能: 加入文字在文字檔後方 */ package my. Java. ch 12; import java. lang. *; import java. io. *; public class ch 12_04 //主類別 { public static void main(String args[]) throws IOException { String str 1 = "費氏數列是一個無限數列";

字元串流的檔案處理 File. Writer類別 1 2 3 4 5 6 7 8 9 10 11

字元串流的檔案處理 File. Writer類別 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 【實用範例12 -5】:複製文字檔。 範例12 -5:ch 12_05. java(隨書光碟 my. Javach 12_05. java) /* 檔名: ch 12_05. java 功能: 複製文字檔 */ package my. Java. ch 12; import java. lang. *; import java. io. *; public class ch 12_05 //主類別 { public static void main(String args[]) throws IOException, File. Not. Found. Exception { char cbuf[] = new char[1]; File. Reader fr = new File. Reader("c: \my. Java\ch 12\file\text 1. txt"); File. Writer fw = new File. Writer("c: \my. Java\ch 12\file\text 3. txt"); int num; String str 1;

字元串流的檔案處理 緩衝區 1 2 3 4 5 6 7 8 9 10 11 12

字元串流的檔案處理 緩衝區 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /* 檔名: ch 12_06. java 功能: 使用緩衝區讀取文字檔 */ package my. Java. ch 12; import java. lang. *; import java. io. *; public class ch 12_06 //主類別 { public static void main(String args[]) throws IOException, File. Not. Found. Exception { String str 1; File. Reader fr = new File. Reader("c: \my. Java\ch 12\file\text 1. txt"); Buffered. Reader buffer. In = new Buffered. Reader(fr); while((str 1=buffer. In. read. Line())!=null) { System. out. println(str 1); } fr. close(); //關檔 } }

字元串流的檔案處理 緩衝區 8 9 10 11 12 13 14 15 16 17 18 19

字元串流的檔案處理 緩衝區 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class ch 12_07 //主類別 { public static void main(String args[]) throws IOException { String str 1 = "費氏數列如下: "; char end. Ch[] = {'C', 'o', 'n', 't', 'i', 'n', 'u', 'e', '. '}; int num. F; File. Writer fw = new File. Writer("c: \my. Java\ch 12\file\text 4. txt"); Buffered. Writer buffer. Out = new Buffered. Writer(fw, 20); buffer. Out. write(str 1); buffer. Out. new. Line(); for(int i=1; i<10; i++) { num. F=Fib(i); buffer. Out. write(num. F+" } buffer. Out. new. Line(); buffer. Out. write(end. Ch); buffer. Out. flush(); fw. close(); //關檔 } //寫入換行字元 "); //int與字串連結, 會自動轉型為字串 //寫入換行字元 //重要, 請看註解

字元串流的檔案處理 緩衝區 31 32 33 34 35 36 37 38 public static int Fib(int

字元串流的檔案處理 緩衝區 31 32 33 34 35 36 37 38 public static int Fib(int n) { if((n==1) || (n==0)) return n; else return Fib(n-1)+Fib(n-2); } } 執行結果 程式執行完畢,會在c: my. Javach 12file目錄下產生text 4. txt檔,內 容如下

位元串流的檔案處理 File. Input. Stream與File. Output. Stream類別 File. Output. Stream的建構子 File. Output. Stream(File file) File.

位元串流的檔案處理 File. Input. Stream與File. Output. Stream類別 File. Output. Stream的建構子 File. Output. Stream(File file) File. Output. Stream(File file, boolean append) File. Output. Stream(File. Descriptor fd. Obj) File. Output. Stream(String name, boolean append) //上述建構子會拋出IOException例外 【說明】: 同樣地,本書僅介紹後兩類建構子,其中,file. Name為代表檔案路徑 及檔名的字串,而append則是指定是否覆蓋檔案原有內容,若欲覆 蓋原有檔案內容,則將之設定為false或不設定;若希望將新資料加 在舊有資料後面,則設定為true。

位元串流的檔案處理 File. Input. Stream與File. Output. Stream類別 1 2 3 4 5 6 7 8

位元串流的檔案處理 File. Input. Stream與File. Output. Stream類別 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 【實用範例12 -8】:透過位元串流複製圖片檔。 範例12 -8:ch 12_08. java(隨書光碟 my. Javach 12_08. java) /* 檔名: ch 12_08. java 功能: 複製檔案 */ package my. Java. ch 12; import java. lang. *; import java. io. *; public class ch 12_08 //主類別 { public static void main(String args[]) throws IOException, File. Not. Found. Exception { byte. Data[] = new byte[1]; File. Input. Stream fi = new File. Input. Stream("c: \my. Java\ch 12\file\pic 1. jpg"); File. Output. Stream fo = new File. Output. Stream("c: \my. Java\ch 12\file\pic 2. jpg"); int file. Size=fi. available();

位元串流的檔案處理 File. Input. Stream與File. Output. Stream類別 17 18 19 20 21 22 23 24

位元串流的檔案處理 File. Input. Stream與File. Output. Stream類別 17 18 19 20 21 22 23 24 int num; while((num = fi. read(byte. Data)) !=-1) //使用迴圈逐一複製每一個位元組 fo. write(byte. Data); System. out. println("檔案大小=" + file. Size + "位元組複製完畢"); fi. close(); //關檔 fo. close(); //關檔 } } pic 1. jpg(隨書光碟 my. Javach 12filepic 1. jpg)

位元串流的檔案處理 緩衝區 1 2 3 4 5 6 7 8 9 10 11 12

位元串流的檔案處理 緩衝區 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 【實用範例12 -9】:使用位元串流搭配緩衝區讀取文字檔內容, 並寫入另一個檔案中。 範例12 -9:ch 12_09. java(隨書光碟 my. Javach 12_09. java) /* 檔名: ch 12_09. java 功能: 複製並輸出檔案 */ package my. Java. ch 12; import java. lang. *; import java. io. *; public class ch 12_09 //主類別 { public static void main(String args[]) throws IOException, File. Not. Found. Exception { File. Input. Stream fi = new File. Input. Stream("c: \my. Java\ch 12\file\text 1. txt"); File. Output. Stream fo = new File. Output. Stream("c: \my. Java\ch 12\file\text 5. txt"); Buffered. Input. Stream buf. In = new Buffered. Input. Stream(fi);

位元串流的檔案處理 緩衝區 16 17 18 19 20 21 22 23 24 25 26 27

位元串流的檔案處理 緩衝區 16 17 18 19 20 21 22 23 24 25 26 27 28 } Buffered. Output. Stream buf. Out = new Buffered. Output. Stream(fo); int file. Size=fi. available(); byte. Data[] = new byte[file. Size]; buf. In. read(byte. Data, 0, file. Size); buf. Out. write(byte. Data, 0, file. Size); String str 1 = new String(byte. Data); //轉換位元組陣列為字串 buf. Out. flush(); //記得要flush, 否則串流內資料不會寫入檔案中 System. out. println("檔案複製完畢, 內容如下"); System. out. println(str 1); fi. close(); //關檔 fo. close(); //關檔 執行結果: } 檔案複製完畢, 內容如下 S 9703501 王大民 89 84 75 S 9703502 郭小志 77 69 87 S 9703503 胡小龍 65 68 77

位元串流的檔案處理 序列化(Serialization) Object. Output. Stream類別與write. Object()方法 由圖 12 -3中,我們可以發現Object. Output. Stream類別繼承自 Output. Stream類別,其建構子及write. Object()方法分別如下:

位元串流的檔案處理 序列化(Serialization) Object. Output. Stream類別與write. Object()方法 由圖 12 -3中,我們可以發現Object. Output. Stream類別繼承自 Output. Stream類別,其建構子及write. Object()方法分別如下: Object. Output. Stream(Output. Stream out) // Object. Output. Stream建構子 void write. Object(Obecjt obj) //寫入物件到位元串流 Object. Input. Stream類別與read. Object()方法 由圖 12 -3中,我們可以發現Object. Input. Stream類別繼承自 Input. Stream類別,其建構子及read. Object()方法分別如下: Object. Input. Stream(Input. Stream in) // Object. Input. Stream建 構子 Object read. Object() //由位元串流讀出物件並回傳

位元串流的檔案處理 12 13 14 15 16 17 18 19 20 21 22 23 24

位元串流的檔案處理 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 序列化(Serialization) { File. Output. Stream fo = new File. Output. Stream("c: \my. Java\ch 12\file 6. tmp"); Object. Output. Stream oos = new Object. Output. Stream(fo); oos. write. Object(new CMy. Student("S 9703501", "王大民", 89, 84, 75)); oos. write. Object(new CMy. Student("S 9703502", "郭小志", 77, 69, 87)); oos. write. Object(new CMy. Student("S 9703503", "胡小龍", 65, 68, 77)); oos. close(); fo. close(); //關檔 } } class CMy. Student implements Serializable //CMy. Student實作Serializable介面 { private String id; private String name; private int score. Computer; private int score. Math; private int score. English; private int score. Sum; public CMy. Student(String str 1, String str 2, int i, int j, int k)

位元串流的檔案處理 35 36 37 38 39 40 41 42 43 44 45 46 47

位元串流的檔案處理 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 序列化(Serialization) { id=str 1; name=str 2; score. Computer=i; score. Math=j; score. English=k; compute. Sum(); } public void compute. Sum() { score. Sum=score. Computer+score. Math+score. English; } public void print. Sum() { //空敘述 } 執行結果(執行完畢產生file 6. tmp檔) }

位元串流的檔案處理 序列化(Serialization) 1 2 3 4 5 6 7 8 9 10 11 12

位元串流的檔案處理 序列化(Serialization) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 【觀念範例12 -11】:使用序列化讀出物件。 範例12 -11:ch 12_11. java(隨書光碟 my. Javach 12_11. java) /* 檔名: ch 12_11. java 功能: 序列化 */ package my. Java. ch 12; import java. lang. *; import java. io. *; public class ch 12_11 //主類別 { public static void main(String args[]) throws IOException, Class. Not. Found. Exception { File. Input. Stream fi = new File. Input. Stream("c: \my. Java\ch 12\file 6. tmp"); Object. Input. Stream ois = new Object. Input. Stream(fi); CMy. Student obj 1;

位元串流的檔案處理 序列化(Serialization) 17 18 19 20 21 22 23 24 25 26 27 28

位元串流的檔案處理 序列化(Serialization) 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 obj 1=(CMy. Student)ois. read. Object(); obj 1. print. Sum(); ois. close(); fi. close(); //關檔 } } class CMy. Student implements Serializable //CMy. Student實作Serializable介面 { private String id; private String name; private int score. Computer; private int score. Math; private int score. English; private int score. Sum; public CMy. Student(String str 1, String str 2, int i, int j, int k) {

位元串流的檔案處理 序列化(Serialization) 40 41 42 43 44 45 46 47 48 49 50 51

位元串流的檔案處理 序列化(Serialization) 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 id=str 1; name=str 2; score. Computer=i; score. Math=j; score. English=k; compute. Sum(); } public void compute. Sum() { score. Sum=score. Computer+score. Math+score. English; } public void print. Sum() //修改函式內容, 但函式不可或缺 { System. out. println(id + " " + name + " 總分=" + score. Sum); } }