National Taiwan University Department of Computer Science and

  • Slides: 48
Download presentation
National Taiwan University Department of Computer Science and Information Engineering Java Conventions I/O基礎 Lecturer:楊昌樺

National Taiwan University Department of Computer Science and Information Engineering Java Conventions I/O基礎 Lecturer:楊昌樺

National Taiwan University Department of Computer Science and Information Engineering Java Conventions Java 原始碼檔案

National Taiwan University Department of Computer Science and Information Engineering Java Conventions Java 原始碼檔案 每一個 Java 原始碼檔案都包含了一個單一的 public class/interface。如果 private classes/interfaces 和 public class 相關,那你應該把他們跟 public class 原始碼檔案放 在一起。public 應該是這個檔案中的第一個 calss/interface。 原始碼的順序 • 起始註解 • package 和 import 敘述 • class 和 interface 宣告

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 起始註解 /*

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 起始註解 /* * 類別名稱 * * 版本資訊 * * 日期 * * 版權宣告 */ package 和 import package 會放在第一行 package tw. com. Graphics; import java. io. *;

National Taiwan University Department of Computer Science and Information Engineering Java Conventions class 和

National Taiwan University Department of Computer Science and Information Engineering Java Conventions class 和 interface 的宣告 class/interface --------------------------------註解 (/**. . . */) 這註解中應該有甚麼資訊。 class/interface 敘述 類別 (static) 變數:首先是 public 的類別變數,然後是 protected,再接著是 package 層級 ( 沒有存取修正子),最後是 private。 實體變數:首先是 public,然後 protected,然後package(沒有存取修正子),最後 是 private。 建構子 方法 方法應該以功能來分群,而不是以存取權限分。

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 縮排 使用

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 縮排 使用 Tab 避免一行長度超過 80 字元 換行 some. Method(long. Expression 1, long. Expression 2, long. Expression 3, long. Expression 4, long. Expression 5); if (( condition 1 && condition 2 ) || ( condition 3 && condition 4) || !( condition 5 && condition 6)) { do. Something. About. It(); }

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 註解 區塊註解

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 註解 區塊註解 單行註解 if (condition) { if ( foo > 1) { /* 處理這個狀況。 */. . . } // 做出雙空翻。. . . } else { return false; // 在這裡解釋為甚麼。 }

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 類別與介面宣告 class

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 類別與介面宣告 class Sample extends Object { int ivar 1; int ivar 2; Sample(int i, nit j) { ivar 1 = i; ivar 2 = j; } int empty. Method() {}. . . }

National Taiwan University Department of Computer Science and Information Engineering Java Conventions return 敘述

National Taiwan University Department of Computer Science and Information Engineering Java Conventions return 敘述 return ( size ? size : default. Size ); if-else 敘述 for( 初始化; 條件; 更新 ) { 敘述; } while 敘述 do-wile 敘述 if (條件) { 敘述; } else { 敘述; }

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 權限 沒有好理由的話,不要讓任何的實體變數或是類別變數

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 權限 沒有好理由的話,不要讓任何的實體變數或是類別變數 是 public 的。 類別變數及方法 避免使用物件來存取類別(static)變數和方法。使用類別名 稱來代替。例如: AClass. class. Method(); // OK an. Object. class. Method(); // 避免! 變數指定 foo. Bar. f. Char = bar. Foo. lchar = 'c'; // 避免! d = ( a = b + c) + r; // 避免!

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 小括號 if

National Taiwan University Department of Computer Science and Information Engineering Java Conventions 小括號 if (a == b && c == d) // 避免! if ((a == b) && (c == d)) // 使用 傳回值 if (boolean. Expression) { return true; } else { return false; } return boolean. Expression; if (條件) { return x; } return y; return (條件 ? x : y);

National Taiwan University Department of Computer Science and Information Engineering 多型在member variable與method的差異 一個物件的成員變數(member variable)

National Taiwan University Department of Computer Science and Information Engineering 多型在member variable與method的差異 一個物件的成員變數(member variable) 是存取自物件所宣告的Class Dog dog 1 = new Small. Dog(); 物件dog 1,被宣告為Dog dog 1. name的內容為Bear成員變數name的內容 但是呼叫此物件的方法(method), 則是執行自此物 件實體的Class

National Taiwan University Department of Computer Science and Information Engineering 範例 Dog. Demo. java

National Taiwan University Department of Computer Science and Information Engineering 範例 Dog. Demo. java public class Dog. Demo{ public static void main(String[] args){ Dog dog 1 = new Small. Dog(); class Dog{ dog 1. name(); public String name; Dog(){this. name="Dog"; } System. out. println(dog 1. name); public void name() }} {System. out. println("I am a dog. "); } } class Small. Dog extends Dog { public String name; Small. Dog(){this. name="Small. Dog"; } public void name() {System. out. println("I am a small dog. "); } }

National Taiwan University Department of Computer Science and Information Engineering Overview of I/O Streams

National Taiwan University Department of Computer Science and Information Engineering Overview of I/O Streams 程式透過資料流(Stream)讀取一連串字元,來源可 以是檔案、記憶體或是網路 程式也可以透過 Stream 的方式,將資料寫出到目 的地

National Taiwan University Department of Computer Science and Information Engineering Overview of I/O Streams

National Taiwan University Department of Computer Science and Information Engineering Overview of I/O Streams Character Streams 用來處理 16 位元資料,如:字元資料(unicode) Reader 和 Writer 是所有 character streams 的 abstract superclasses

National Taiwan University Department of Computer Science and Information Engineering Overview of I/O Streams

National Taiwan University Department of Computer Science and Information Engineering Overview of I/O Streams Byte Streams 用來處理 8 位元資料,如:執行檔、圖檔和聲音檔 Input. Stream 和 Output. Stream 是所有 byte streams 的 abstract superclasses

National Taiwan University Department of Computer Science and Information Engineering Overview of I/O Streams

National Taiwan University Department of Computer Science and Information Engineering Overview of I/O Streams Understanding the IO Superclasses Reader 與 Input. Stream 有非常類似的 API,只是處理的資 料型態不同,如: • Reader 中的 methods int read() int read(char cbuf[], int offset, int length) • Input. Stream 中的 methods int read() int read(byte buf[], int offset, int length)

National Taiwan University Department of Computer Science and Information Engineering Overview of I/O Streams

National Taiwan University Department of Computer Science and Information Engineering Overview of I/O Streams Writer 與 Output. Stream • Writer 中的 methods int write(int c) int write(char cbuf[], int offset, int length) • Output. Stream 中的 methods int write(int c) int write(byte buf[], int offset, int length) 所以的 Streams 都一樣,在物件建立之後,就會自動開啟, 而呼叫 close() 就可以關閉資料流

National Taiwan University Department of Computer Science and Information Engineering Using the Streams I/O

National Taiwan University Department of Computer Science and Information Engineering Using the Streams I/O 類型 資料流類別 說明 File. Reader File. Writer 功能在於存取檔案或檔 案系統的內容 檔案(File) File. Input. Stream File. Output. Stream Piped. Reader Piped. Writer 管線(Pipe) 將某個程式(或 Thread) 的輸出導入另一個程式 的輸入 Piped. Input. Stream Piped. Output. Stream 未提供 串接(Concatenate) Sequence. Input. Stream 將多個 input stream 串接 到同一個 input stream

National Taiwan University Department of Computer Science and Information Engineering Using the Streams I/O

National Taiwan University Department of Computer Science and Information Engineering Using the Streams I/O 類型 資料流類別 說明 Buffered. Reader Buffered. Writer 讀寫時為資料緩衝區, 可以減少存取原始資料 的次數 緩衝(Buffer) Buffered. Input. Stream Buffered. Output. Stream Filter. Reader Filter. Writer 過濾(Filter) 這幾個類別都是 abstract class,定義了過濾讀寫 資料的介面 Filter. Input. Stream Filter. Output. Stream 未提供 物件序列化 (Object Serialization) Object. Input. Stream Object. Output. Stream 用來做物件序列化( Object Serialization)的 動作

National Taiwan University Department of Computer Science and Information Engineering File 檔案與目錄之操作 在 Java

National Taiwan University Department of Computer Science and Information Engineering File 檔案與目錄之操作 在 Java 中建立 File 物件,就可以取得檔案的或資料夾的 相關訊息,如:File f = new File(String); File() 建構元參數是檔案或是資料夾的路徑 例如: import java. io. *; File f = new File(“haha. txt”);

National Taiwan University Department of Computer Science and Information Engineering File 的相關方法 public boolean

National Taiwan University Department of Computer Science and Information Engineering File 的相關方法 public boolean is. Directory() • 傳回目前 java. io. File 內所包含的這個名稱是否為一個資料夾 public boolean is. File() • 傳回目前 java. io. File 內的名稱是否為一個檔案 public String[] list() • 傳回目前資料夾內所包含的資料夾名稱及檔案名稱

National Taiwan University Department of Computer Science and Information Engineering 檔案清單範例 Example: Dir. java

National Taiwan University Department of Computer Science and Information Engineering 檔案清單範例 Example: Dir. java import java. io. *; public class Dir { public static void main(String[] args) { String[] filenames; File f = new File(args[0]); if ( f. is. Directory() ) { filenames = f. list(); for(int i=0; i< filenames. length; i++) System. out. println(filenames[i]); } else System. out. println(f + "is not a directory"); } }

National Taiwan University Department of Computer Science and Information Engineering 顯示檔案資訊 相關方法 public boolean

National Taiwan University Department of Computer Science and Information Engineering 顯示檔案資訊 相關方法 public boolean exists(): 檢查檔案是否存在 public boolean can. Read(): 是否可讀 public boolean can. Write(): 是否可寫 public get. Name(): 傳回該檔案的名稱 public long length(): 傳回檔案大小 public long last. Modified(): 傳回檔案修改時間 public get. Path(): 取得該檔案所屬的資料夾名稱 public get. Parent(): 取得該檔案所屬的父資料夾名稱 public get. Absolute. File(): 傳回該檔案的絕對路徑

National Taiwan University Department of Computer Science and Information Engineering 顯示檔案資訊範例 Exmaple: File. Info.

National Taiwan University Department of Computer Science and Information Engineering 顯示檔案資訊範例 Exmaple: File. Info. java File f = new File(args[0]); if ( f. exists() ) { if ( f. is. File() ) System. out. print("File: "); else if(f. is. Directory()) System. out. print("Directory: "); System. out. println(f. get. Absolute. File()); System. out. println("Length: " + f. length()); System. out. println("Readable: " + f. can. Read()); System. out. println("Writable: " + f. can. Write()); } else System. out. println(f + " does not exist!");

National Taiwan University Department of Computer Science and Information Engineering 更改檔案名稱 相關方法 public boolean

National Taiwan University Department of Computer Science and Information Engineering 更改檔案名稱 相關方法 public boolean rename. To(File dest) • 功能:更改檔名 • 傳回值:true 成功,false 失敗

National Taiwan University Department of Computer Science and Information Engineering 更改檔案名稱範例 Example: Rename. File.

National Taiwan University Department of Computer Science and Information Engineering 更改檔案名稱範例 Example: Rename. File. java File fs = new File(args[0]); File fd = new File(args[1]); if ( fs. exists() ) { if ( !fd. exists() ) { if (fs. rename. To(fd)) { System. out. println(fs. get. Name() + " --> " + fd. get. Name()); System. out. println("1 file(s) has been renamed!"); } } else System. out. println("Target name already exists!"); } else System. out. println("Wrong source name!");

National Taiwan University Department of Computer Science and Information Engineering File 相關方法 刪除檔案 public

National Taiwan University Department of Computer Science and Information Engineering File 相關方法 刪除檔案 public boolean delete() • 功能:刪除檔案 • 傳回值:true 刪除成功;false 失敗 建立資料夾 boolean mkdir() boolean mkdirs() • 建立資料夾,成功傳回 true;失敗 false

National Taiwan University Department of Computer Science and Information Engineering 建立資料夾範例 Example: Make. Dir.

National Taiwan University Department of Computer Science and Information Engineering 建立資料夾範例 Example: Make. Dir. java import java. io. *; public class Make. Dir { public static void main(String[] args) { File dir = new File(args[0]); if ( !dir. exists() ) { boolean success = dir. mkdir(); // boolean success = dir. mkdirs(); System. out. println("Create "+dir+" is successed!!"); } else System. out. println(dir + " is already exist!!!"); } }

National Taiwan University Department of Computer Science and Information Engineering File. Reader / File.

National Taiwan University Department of Computer Science and Information Engineering File. Reader / File. Writer 可利用 File 物件來當作 File. Reader / File. Writer 的建 構元參數 如: File input. File = new File(“test. txt”); File. Reader in = new File. Reader(input. File); 建立 File. Reader 的物件之後可用 read() 來讀入 16 -bit 字元 建立 File. Writer 的物件之後可用 write() 來寫出 16 -bit 字元

National Taiwan University Department of Computer Science and Information Engineering File. Reader / File.

National Taiwan University Department of Computer Science and Information Engineering File. Reader / File. Writer Example: Copy. java … File input. File = new File("farrago. txt"); File output. File = new File("outagain. txt"); File. Reader in = new File. Reader(input. File); File. Writer out = new File. Writer(output. File); int c; while ((c = in. read()) != -1) out. write(c); in. close(); out. close(); …

National Taiwan University Department of Computer Science and Information Engineering Wrap Streams – 包裹

National Taiwan University Department of Computer Science and Information Engineering Wrap Streams – 包裹 Stream 以一種 stream 類別包裹另一種 stream 類別的情形, 這樣可以將各種不同資料流中好用的 methods 合 併使用 在讀取資料時,可以利用 Buffered. Reader 將來源 包裹(wrap)起來,如此一來便可以使用呼叫 Buffered. Reader 中提供的 read. Line() 來讀入一整 行的資料

National Taiwan University Department of Computer Science and Information Engineering Java讀寫檔案 Buffered. Reader 和

National Taiwan University Department of Computer Science and Information Engineering Java讀寫檔案 Buffered. Reader 和 Buffered. Writer 程式做 IO 動作時,使用 8 K 左右大小之 Buffer 可得到最佳效能 Buffered. Reader 和 Buffered. Writer 已經內建 Buffer 幫你做實際 IO 傳輸 buffer 處理 示範程式 • 使用 Buffered. Reader 讀出檔案內容

National Taiwan University Department of Computer Science and Information Engineering 示範程式 Example: Buf. Reader.

National Taiwan University Department of Computer Science and Information Engineering 示範程式 Example: Buf. Reader. Demo. java String file = "farrago. txt"; File name = new File(file); if ( name. exists() ) { Buffered. Reader input = new Buffered. Reader( new File. Reader(name)); String str; while ( (str = input. read. Line()) != null ) // 判別是否讀到檔尾 System. out. println(str); input. close(); } else System. out. println("File [" + name + "] doesn't exist. "); }

National Taiwan University Department of Computer Science and Information Engineering Buffered. Reader使用法 Constructor: Buffered.

National Taiwan University Department of Computer Science and Information Engineering Buffered. Reader使用法 Constructor: Buffered. Reader(Reader in); Reader 是一個 abstract 類別,不能被 new 出來,只 能被繼承使用 查API可知道 Reader 被 File. Reader 繼承。 而 File. Reader 有一 Constructor 為 File. Reader(String filename) 因此我們可以 new 一個 File. Reader,當作參數傳入 Buffered. Reader 之 Constructor

National Taiwan University Department of Computer Science and Information Engineering Buffered. Writer使用法 Constructor: Buffered.

National Taiwan University Department of Computer Science and Information Engineering Buffered. Writer使用法 Constructor: Buffered. Writer(Writer out); Writer 是一個 abstract 類別,不能被 new 出來,只 能被繼承使用 查 API 可知到 Writer 被 File. Writer 繼承。 而 File. Writer 有一 Constructor 為 File. Writer(String filename) 因此我們可以 new 一個 File. Writer,當作參數傳入 Buffered. Writer 之 Constructor

National Taiwan University Department of Computer Science and Information Engineering 為何Java如此大費周章 使用Buffered. Reader為何還要先產生File. Reader,再

National Taiwan University Department of Computer Science and Information Engineering 為何Java如此大費周章 使用Buffered. Reader為何還要先產生File. Reader,再 指向檔案名稱? Buffered. Reader br = new Buffered. Reader(new File. Reader(“Test. txt”)); 因為 Buffered. Reader 和 Buffered. Writer 不只可以做檔案處 理之功能 只需置換 Buffered. Reader 之建構子,即可使用相同的API 做出讀取鍵盤輸入和網路傳輸等功能 請注意 Buffered. Reader 和 Buffered. Writer 的建構子 為 Reader 和 Writer 之類別

National Taiwan University Department of Computer Science and Information Engineering Input. Stream / Output.

National Taiwan University Department of Computer Science and Information Engineering Input. Stream / Output. Stream 在 Java 中,相對應於 Reader / Writer,用來讀取和寫入位 元或位元陣列 提供的方法: int read() -- 讀取一個位元 int read(byte[]) – 讀取位元陣列,當資料結尾時,傳回 -1 int read(byte[], int) void write(int) – 寫入整數 void write(byte[]) – 寫入 1 個位元組陣列 void write(byte[], int)

National Taiwan University Department of Computer Science and Information Engineering Input. Stream / Output.

National Taiwan University Department of Computer Science and Information Engineering Input. Stream / Output. Stream 對於位元的檔案 IO,Java 程式是開啟 File. Output. Stream 和 File. Input. Stream 串流,如: File. Output. Stream output = new File. Output. Stream(file); … File. Input. Stream input = new File. Input. Stream(name); … 參數可以是檔案路徑字串,或是 File 物件

National Taiwan University Department of Computer Science and Information Engineering Input. Stream / Output.

National Taiwan University Department of Computer Science and Information Engineering Input. Stream / Output. Stream Example: In. Out. Stream. Demo. java File. Input. Stream input = new File. Input. Stream(name); File. Output. Stream output = new File. Output. Stream("clone. jpg"); // 當讀取的檔案不是文字檔的話,用 File. Reader 來讀取會有問題 //File. Reader input = new File. Reader(name); //File. Writer output = new File. Writer("clone. jpg"); int b; while ( (b = input. read()) != -1 ) { output. write(b); System. out. println(b); // 顯示出一個 pixel 的數值 } output. close(); input. close();