field 11 field 12 field 13 field 1

  • Slides: 53
Download presentation

field 11 field 12 field 13 ... field 1 n record 1 field 22

field 11 field 12 field 13 ... field 1 n record 1 field 22 field 23 ... fiel 2 n record 2 . . . fieldm 1 Java語言實務 fieldm 2 . . . fieldm 3 ... 4 field mn record m 輸入及輸出處理 檔 案

 • 欄位 – Full name, Party, Born, Residence, Education, Religion • 資料記錄 George

• 欄位 – Full name, Party, Born, Residence, Education, Religion • 資料記錄 George W. Bush, Republican, July 6 1946, Austin Texas, M. B. A, Methodist Albert Gore Jr. , Democrat, March 31 1948, Washington D. C. , B. A. , Baptist Harry Browne, Libertarian, June 17 1933, Franklin, Tennessee, High school, nil Patrick J. Buchanan, Reform, November 2 1938, Mc. Lean Virginia, M. S. , Roman Catholic Ralph Nader, Green, February 27 1934, Washington D. C. , L. L. B, Christian Howard Phillips, Constitution, February 3 1941, Fairfax Virginia, B. A. , Protestant Java語言實務 5 輸入及輸出處理

 • 三個建構子 File(String directory. Path) File(String directory. Path, String filename) File(File dir. Obj,

• 三個建構子 File(String directory. Path) File(String directory. Path, String filename) File(File dir. Obj, String filename) • File建構子的使用範例 File file 1 = new File(“c: /”); File file 2 = new File(“c: /”, “config. sys”); File file 3 = new File(file 1, “config. sys); Java語言實務 8 輸入及輸出處理

程式Ex 10_1使用File類別 import java. io. File; import java. util. Date; class File. Object {public

程式Ex 10_1使用File類別 import java. io. File; import java. util. Date; class File. Object {public static void main(String args[]) throws java. io. IOException { File file; //宣告一個型態為File的變數 file = new File("c: /config. sys"); //建立File物件 //讀取並列印有關檔案config. sys的屬性 System. out. println("Reading and printing attributes of config. sys"); System. out. println(" Exists: " + file. exists()); System. out. println(" File Name: " + file. get. Name()); System. out. println(" Canonical File Name: " + file. get. Canonical. File()); System. out. println(" Writable: " + file. can. Write()); System. out. println(" Length: " + file. length()); System. out. println(" Hidden: " + file. is. Hidden()); System. out. println(" is. File: " + file. is. File()); System. out. println(" is. Directory: " + file. is. Directory()); System. out. println(" Last. Modified: " + new Date(file. last. Modified())); } } Java語言實務 9 輸入及輸出處理

 • 執行結果 Reading and printing attributes of config. sys Exists: true File Name:

• 執行結果 Reading and printing attributes of config. sys Exists: true File Name: config. sys Canonical File Name: C: CONFIG. SYS Writable: true Length: 65 Hidden: false is. File: true is. Directory: false Last. Modified: Thu Sep 14 14: 18: 24 GMT+08: 00 2000 Java語言實務 10 輸入及輸出處理

程式Ex 10_2 使用Filename. Filter類別(1/3) import java. io. *; class Name. Filter implements Filename. Filter

程式Ex 10_2 使用Filename. Filter類別(1/3) import java. io. *; class Name. Filter implements Filename. Filter // 必需implement Filename. Filter介面 { String file. Extention; // 存放檔案副檔名 public Name. Filter (String file. Extention) { this. file. Extention = file. Extention; } // 必需在Filename. Filter介面改寫accept方法 public boolean accept(File dir, String file. Name) // file. Name代表目錄內的檔名 { return file. Name. ends. With(file. Extention); } // 檢查檔案的副檔名是否為file. Extention的值 } Java語言實務 11 輸入及輸出處理

程式Ex 10_2 使用Filename. Filter類別(2/3) public class Name. Filter. Demo { public static void main

程式Ex 10_2 使用Filename. Filter類別(2/3) public class Name. Filter. Demo { public static void main (String args[]) throws java. io. IOException { String extention; // 如果引用程式時沒給定副檔名參數 if (args. length==0) extention = ". java"; // 將變數extention預設成. java // 否則將變數extention設定成引用程式時所給定副檔名 else extention = args[0]; String dir. Name = "c: /my documents/my pictures"; // 產生代表目錄的File物件 File file = new File(dir. Name); Java語言實務 12 輸入及輸出處理

程式Ex 10_2 使用Filename. Filter類別(3/3) // 產生用以篩選檔名的Filename. Filter物件 // 並將extention所代表的副檔名做為篩選條件 Filename. Filter filter = new

程式Ex 10_2 使用Filename. Filter類別(3/3) // 產生用以篩選檔名的Filename. Filter物件 // 並將extention所代表的副檔名做為篩選條件 Filename. Filter filter = new Name. Filter(extention); // 列出目錄下符合篩選條件的檔名 String file. List[] = file. list(filter); // 如果沒有符合條件的檔案則印出提示訊息 if (file. List. length==0) {System. out. println("No file in dir. '" + file. get. Canonical. Path() + "' ends with the " + extention + " extention!"); System. out. println("Syntax: java Name. Filter. Demo <extention>"); } else // 否則逐一列印出符合篩選條件的檔名 { System. out. println("Files in dir. '" + file. get. Canonical. Path() + "' that ends with " + extention + ": "); for (int i=0; i<file. List. length; i++) System. out. println(" " + file. List[i]); } } } Java語言實務 13 輸入及輸出處理

在命令模式下以 “java Name. Filter. Demo. ufo” 之 執行可能結果為: Files in dir. 'C: My DocumentsMy

在命令模式下以 “java Name. Filter. Demo. ufo” 之 執行可能結果為: Files in dir. 'C: My DocumentsMy Pictures' that ends with. ufo: Taiwan_1. ufo ROC_Flag_1. ufo Java語言實務 14 輸入及輸出處理

四個主要類別的子類別 Input. Stream Byte. Array Input. Stream Filter Input. Stream File Input. Stream Piped

四個主要類別的子類別 Input. Stream Byte. Array Input. Stream Filter Input. Stream File Input. Stream Piped Input. Stream Data Input. Stream Java語言實務 Buffered Input. Stream 17 Sequence String. Buffer Object Input. Stream Push. Back Input. Stream Line. Number Input. Stream 輸入及輸出處理

Output. Stream Byte. Array Output. Stream Java語言實務 File Output. Stream Filter Output. Stream Piped

Output. Stream Byte. Array Output. Stream Java語言實務 File Output. Stream Filter Output. Stream Piped Output. Stream Buffered Output. Stream Data Output. Stream Print. Stream 18 Object Output. Stream 輸入及輸出處理

Reader Buffered Reader Java語言實務 Char. Array Reader Filter. Reader Input. Stream Piped. Reader String.

Reader Buffered Reader Java語言實務 Char. Array Reader Filter. Reader Input. Stream Piped. Reader String. Reader 19 輸入及輸出處理

Writer Buffered Writer Java語言實務 Char. Array Writer Filter. Writer Output. Stream Piped. Writer 20

Writer Buffered Writer Java語言實務 Char. Array Writer Filter. Writer Output. Stream Piped. Writer 20 String. Writer 輸入及輸出處理

 • File. Input. Stream – 為Input. Stream的子類別 – 目的是在建立連結至外部檔案的輸入通道 – 主要有二個建構子 File. Input.

• File. Input. Stream – 為Input. Stream的子類別 – 目的是在建立連結至外部檔案的輸入通道 – 主要有二個建構子 File. Input. Stream(String file. Path) File. Input. Stream(File file. Object) – 會觸發File. Not. Found. Exception Java語言實務 24 輸入及輸出處理

程式 Ex 10_3 使用File. Input. Stream(1/2) import java. io. *; class File. Input. Stream.

程式 Ex 10_3 使用File. Input. Stream(1/2) import java. io. *; class File. Input. Stream. Demo { public static void main(String args[]) throws Exception { int file. Size; String file. Name; File. Input. Stream fis; // 判斷是否有在命令行給定參數 if (args. length == 0) { System. out. println("Syntax: java File. Input. Stream. Demo <filename>"); System. exit(1); } // 第一個參數即為輸入檔案名稱 file. Name = args[0]; // 開啟一個連至外部檔案的輸入通道 fis=new File. Input. Stream(file. Name); Java語言實務 25 輸入及輸出處理

程式 Ex 10_3 使用File. Input. Stream(2/2) file. Size = fis. available(); // 取得檔案大小 if

程式 Ex 10_3 使用File. Input. Stream(2/2) file. Size = fis. available(); // 取得檔案大小 if ((file. Size <= 0) || (file. Size>1024)) // 判斷檔案大小是否在限定的範圍之內 System. out. println("Incorrect File Size!"); else { byte b[] = new byte[file. Size]; // 宣告存放檔案內容的陣列 System. out. println("File Name: " + file. Name); System. out. println("File Size: " + file. Size); fis. read(b); // 讀取檔案並將讀取之結果放入陣列b之中 for (int i=0; i<file. Size; i++) // 將讀取之內容列印在螢幕上 System. out. print((char)b[i]); } } // end of main } // end of File. Input. Stream. Demo Java語言實務 26 輸入及輸出處理

 • 在命令模式下鍵入 “java File. Input. Stream. Demo c: autoexec. bat” 執行程式 的可能結果之一為: File

• 在命令模式下鍵入 “java File. Input. Stream. Demo c: autoexec. bat” 執行程式 的可能結果之一為: File Name: c: autoexec. bat File Size: 166 @ECHO OFF c: maestro. com PATH C: BITWARE if not exist c: save 2 dsk. bin c: toolsphdisk. exe /c /f < c: toolsyes PATH=%PATH%; "C: Program FilesMts"; "c: jdk 1. 3. 1bin“ Java語言實務 27 輸入及輸出處理

 • File. Output. Stream – 為Output. Stream的子類別 – 目的是在建立連結至外部檔案的輸出通道 – 主要也有二個建構子 • File.

• File. Output. Stream – 為Output. Stream的子類別 – 目的是在建立連結至外部檔案的輸出通道 – 主要也有二個建構子 • File. Output. Stream(String file. Path) • File. Output. Stream(File file. Object) – 會觸發IOException或Security. Exception – 原已存在的檔案內容將會被覆蓋掉 Java語言實務 28 輸入及輸出處理

程式Ex 10_4 使用File. Output. Stream(1/2) import java. io. *; class File. Output. Stream. Demo

程式Ex 10_4 使用File. Output. Stream(1/2) import java. io. *; class File. Output. Stream. Demo { public static void main(String args[]) throws Exception { String file. Name; String line; int max. Line. Length = 256; byte b[] = new byte[max. Line. Length]; File. Output. Stream fos; if (args. length == 0) // 判斷是否有在命令行給定參數 { System. out. println("Syntax: java File. Output. Stream. Demo <filename>"); System. exit(1); } file. Name = args[0]; // 第一個參數即為輸出檔案名稱 fos=new File. Output. Stream(file. Name); Java語言實務 29 輸入及輸出處理

程式Ex 10_4 使用File. Output. Stream(2/2) // 設定要輸出至檔案的內容 line = "Over the thousands of years

程式Ex 10_4 使用File. Output. Stream(2/2) // 設定要輸出至檔案的內容 line = "Over the thousands of years that people have conducted business nr" + "with one another, they have adopted the tools and technologies nr" + "that became available. nr"; // 將字串物件line的內容轉成位元組並將轉換之結果存入陣列b中 // 陣列b將存有字串中各字元的Ascii值 line. get. Bytes(0, line. length(), b, 0); // 將陣列b之內容寫入外部檔案中 for (int i=0; i<line. length(); i++) fos. write(b[i]); fos. close(); } // end of main } // end of File. Output. Stream. Demo Java語言實務 30 輸入及輸出處理

 • 在命令模式下鍵入 “java Fileo. Output. Stream. Demo Hello. txt” 執行程式 後,檔案”Hello. txt”內容為 Over

• 在命令模式下鍵入 “java Fileo. Output. Stream. Demo Hello. txt” 執行程式 後,檔案”Hello. txt”內容為 Over the thousands of years that people have conducted business with one another, they have adopted the tools and technologies that became available. Java語言實務 31 輸入及輸出處理

程式Ex 10_5使用Data. Output. Stream輸出資料 import java. io. *; import java. util. Date; class Data.

程式Ex 10_5使用Data. Output. Stream輸出資料 import java. io. *; import java. util. Date; class Data. Output. Stream. Demo { public static void main(String args[])throws Exception { // 宣告要存放基本資料所需之變數 String full. Name, born, party, residence, education, religion; boolean married; int age; File. Output. Stream fos; Data. Output. Stream dos; // 開啟輸出通道 fos = new File. Output. Stream("personnel. dat"); dos = new Data. Output. Stream(fos); Java語言實務 37 輸入及輸出處理

// 設定各變數之值 full. Name = "George W. Bush"; born = "July 6 1946"; married

// 設定各變數之值 full. Name = "George W. Bush"; born = "July 6 1946"; married = true; age = 54; party = "Republican"; residence = "Austin Texas"; education = "M. B. A. "; religion = "Methodist"; // 以UTF格式依序將字串資料寫入外部檔案 dos. write. UTF(full. Name); dos. write. UTF (born. to. String()); dos. write. Boolean(married); // 布林值佔一個byte的儲存空間 dos. write. Int(age); // 數值類型的值是以其對應之二進位值寫出 dos. write. UTF (party); dos. write. UTF (residence); dos. write. UTF (education); dos. write. UTF(religion); dos. flush(); // 確定所有資料都已寫出 dos. close(); // 關閉輸出通道 } } Java語言實務 38 輸入及輸出處理

程式Ex 10_6使用Data. Input. Stream讀取資料 import java. io. *; import java. util. Date; class Data.

程式Ex 10_6使用Data. Input. Stream讀取資料 import java. io. *; import java. util. Date; class Data. Input. Stream. Demo { public static void main(String args[])throws Exception { String full. Name, party, residence, education, religion; int age; String born; boolean married; File. Input. Stream fis; Data. Input. Stream dis; fis = new File. Input. Stream("personnel. dat"); // 開啟輸入通道 dis = new Data. Input. Stream(fis); Java語言實務 39 輸入及輸出處理

full. Name = dis. read. UTF(); // 以UTF格式讀取資料 born = dis. read. UTF(); married

full. Name = dis. read. UTF(); // 以UTF格式讀取資料 born = dis. read. UTF(); married = dis. read. Boolean(); // 讀取布林值 age = dis. read. Int(); // 讀取整數值 party = dis. read. UTF(); residence = dis. read. UTF(); education = dis. read. UTF(); religion = dis. read. UTF(); // 將讀入之資料列印在螢幕上 System. out. println("Full Name: " + full. Name); System. out. println("Born: " + born); System. out. println("Married: " + married); System. out. println("Age: " + age); System. out. println("Party: " + party); System. out. println("Residence: " + residence); System. out. println("Education: " + education); System. out. println("Religion: " + religion); } } Java語言實務 40 輸入及輸出處理

 • 程式Ex 10_5的執行結果如下: Full Name: George W. Bush Born: July 6 1946 Married:

• 程式Ex 10_5的執行結果如下: Full Name: George W. Bush Born: July 6 1946 Married: true Age: 54 Party: Republican Residence: Austin Texas Education: M. B. A. Religion: Methodist Java語言實務 41 輸入及輸出處理

 • Java各種資料型態所佔的儲存空間大小 Boolean Byte Char Short int 4 Long float Double UTF Java語言實務

• Java各種資料型態所佔的儲存空間大小 Boolean Byte Char Short int 4 Long float Double UTF Java語言實務 1 byte 2 bytes 8 bytes 4 bytes 8 bytes 變動長度 43 輸入及輸出處理

程式Ex 10_7隨機檔案存取範例(1/4) import java. io. *; class Candidate //候選人類別 { private String full. Name;

程式Ex 10_7隨機檔案存取範例(1/4) import java. io. *; class Candidate //候選人類別 { private String full. Name; //候選人基本資料 private String born; private String party; public static int full. Name_SIZE = 32; //各欄位字元長度 public static int born_SIZE = 12; public static int party_SIZE = 24; // 每筆基本資料所需之儲存空間大小 public static int RECORD_SIZE = 2*full. Name_SIZE+2*born_SIZE+2 * party_SIZE; public Candidate(String full. Name, String born, String party) // 建構子 { this. full. Name = full. Name; this. born = born; this. party = party; } public Candidate() {} // 建構子 Java語言實務 45 輸入及輸出處理

程式Ex 10_7隨機檔案存取範例(2/4) public String get_full. Name() // 回傳基本資料之各欄位值 {return full. Name; } public String

程式Ex 10_7隨機檔案存取範例(2/4) public String get_full. Name() // 回傳基本資料之各欄位值 {return full. Name; } public String get_born() {return born; } public String get_party() {return party; } public void write. Data(Random. Access. File out) // 將基本資料寫至檔案 throws IOException { write. Fixed. String(out, full. Name_SIZE); write. Fixed. String(out, born_SIZE); write. Fixed. String(out, party_SIZE); } // end of write. Data public void read. Data(Random. Access. File in) // 將基本資料從檔案讀出 throws IOException { full. Name = read. Fixed. String(in, full. Name_SIZE); born = read. Fixed. String(in, born_SIZE); party = read. Fixed. String(in, party_SIZE); } // end of read. Data Java語言實務 46 輸入及輸出處理

程式Ex 10_7隨機檔案存取範例(3/4) // 以固定長度將字串寫至檔案 private void write. Fixed. String(Random. Access. File out, String s,

程式Ex 10_7隨機檔案存取範例(3/4) // 以固定長度將字串寫至檔案 private void write. Fixed. String(Random. Access. File out, String s, int size) throws IOException { for (int i=0; i<size; i++) { if (i<s. length()) out. write. Char(s. char. At(i)); else out. write. Char(0); } } // end of write. Fixed. String // 從檔案讀入固定長度的字串 private String read. Fixed. String(Random. Access. File in, int size) throws IOException { String. Buffer s = new String. Buffer(size); int i; for (i=0; i<size; i++) { char c = in. read. Char(); if (c==0) break; else s. append(c); } in. skip. Bytes(2*(size-i-1)); return(s. to. String()); } // end of read. Fixed. String } // end of class Candidate Java語言實務 47 輸入及輸出處理

程式Ex 10_7隨機檔案存取範例(4/4) class Random. Access. Demo {public static void main(String args[]) throws Exception {Candidate

程式Ex 10_7隨機檔案存取範例(4/4) class Random. Access. Demo {public static void main(String args[]) throws Exception {Candidate x, y; Random. Access. File raf; // 建立可同時讀寫隨機檔案內容的通道 raf = new Random. Access. File("candidate. X. dat", "rw"); // 產生並將資料寫入檔案 x = new Candidate("George W. Bush", "July 6 1946", "Republican"); x. write. Data(raf); x = new Candidate("Albert Gore Jr. ", "March 31 1948", "Democrat"); x. write. Data(raf); x = new Candidate("Harry Browne", "June 17 1933", "Libertarian"); x. write. Data(raf); x = null; y= new Candidate(); // 反向讀取各筆記錄內容並顯示在螢幕上 for (int i=2; i>=0; i--) { raf. seek(i*Candidate. RECORD_SIZE); y. read. Data(raf); System. out. println(y. get_full. Name()+", "+y. get_born()+ ", " +y. get_party()); } } // end of main } // end of Random. Access. Demo Java語言實務 48 輸入及輸出處理

 • 程式Ex 10_7執行之結果為: Harry Browne, June 17 1933, Libertarian Albert Gore Jr. ,

• 程式Ex 10_7執行之結果為: Harry Browne, June 17 1933, Libertarian Albert Gore Jr. , March 31 194, Democrat George W. Bush, July 6 1946, Republican Java語言實務 49 輸入及輸出處理

程式Ex 10_8物件的輸入及輸出(1/2) import java. io. *; class Candidate implements Serializable { // 存放候選人基本資料的類別 private

程式Ex 10_8物件的輸入及輸出(1/2) import java. io. *; class Candidate implements Serializable { // 存放候選人基本資料的類別 private String full. Name, born, party, residence; private int age; private boolean married; public Candidate(String full. Name, String born, int age, boolean married, String party, String residence) { this. full. Name = full. Name; this. born = born; this. age = age; this. married = married; this. party = party; this. residence = residence; } public String to. String() // 自定的to. String()方法 { return (full. Name+", "+born+", "+age+", "+married+", "+party+", "+residence); } } //End of class Candidate Java語言實務 51 輸入及輸出處理

程式Ex 10_8物件的輸入及輸出(2/2) class Object. Stream. Demo {public static void main(String[] args) throws Exception {

程式Ex 10_8物件的輸入及輸出(2/2) class Object. Stream. Demo {public static void main(String[] args) throws Exception { Candidate[] candidates = new Candidate[2]; candidates[0] = new Candidate("George W. Bush", "July 6 1946", 54, true, "Republican", "Austin Texas"); candidates[1] = new Candidate("Albert Gore Jr. ", "March 31 1948", 52, true, "Democrat", " Mc. Lean Virginia"); // 開啟物件輸出通道 Object. Output. Stream oos; oos = new Object. Output. Stream(new File. Output. Stream("candidates. dat")); oos. write. Object(candidates); // 將物件陣列的所有物件內容一次寫出 oos. close(); // 關閉物件輸出通道 candidates = null; // 開啟物件輸入通道 Object. Input. Stream ois; ois = new Object. Input. Stream(new File. Input. Stream("candidates. dat")); // 讀入物件陣列 candidates = (Candidate[])ois. read. Object(); // 注意型態轉換成Candidate[] System. out. println("Candidate Details: "); // 將讀入的物件內容顯示在螢幕上 for (int i=0; i<candidates. length; i++) System. out. println(" Candidate " + i + ": " + candidates[i]); } // end of main } // end of Object. Stream. Demo Java語言實務 52 輸入及輸出處理

 • 程式Ex 10_8的輸出結果為: Candidate Details: Candidate 0: George W. Bush, July 6 1946,

• 程式Ex 10_8的輸出結果為: Candidate Details: Candidate 0: George W. Bush, July 6 1946, 54, true, Republican, Austin Texas Candidate 1: Albert Gore Jr. , March 31 1948, 52, true, Democrat, Mc. Lean Virginia Java語言實務 53 輸入及輸出處理