File IO n File Output Stream n File

  • Slides: 7
Download presentation

File I/O n 使用File. Output. Stream寫入檔案 n 建立欲寫入之檔案: � File file = new File

File I/O n 使用File. Output. Stream寫入檔案 n 建立欲寫入之檔案: � File file = new File (“sample. data”); n 建立一個輸出流(File. Output. Stream)物件讓 資料流入: File. Output. Stream out = new File. Output. Stream(file); n 利用write()方法將值寫入到file內: out. write(by); n 使用out. close(); 方法來關閉資料流 。

File I/O n 使用File. Input. Stream讀取檔案 n 建立欲讀入之檔案: File file = new File("input. Demo

File I/O n 使用File. Input. Stream讀取檔案 n 建立欲讀入之檔案: File file = new File("input. Demo 1. java"); n 建立一個輸入流( File. Input. Stream )物 件來將資料讀入: � File. Input. Stream in = new File. Input. Stream(file); n 利用read()方法將file內的值讀入: in. read(by); n 使用in. close(); 方法來關閉資料流 。

File I/O n 使用Print. Writer來寫入檔案 n 建立欲寫入之檔案 File file = new File("fiod 3. txt");

File I/O n 使用Print. Writer來寫入檔案 n 建立欲寫入之檔案 File file = new File("fiod 3. txt"); n 建立一個輸出流(File. Output. Stream)物 件讓資料流入: File. Output. Stream out = new File. Output. Stream(file); n 建立Print. Writer物件來寫入資料流: � Print. Writer pw = new Print. Writer(out); n 使用println()方法來寫入檔案: � pw. println(12345);

File I/O n 使用File. Reader來讀取檔案 n 建立欲寫入之檔案 � File file = new File("fiod 3.

File I/O n 使用File. Reader來讀取檔案 n 建立欲寫入之檔案 � File file = new File("fiod 3. txt"); n 建立File. Reader物件來讀取檔案: � File. Reader file. Reader = new File. Reader(file); n 使用Buffered. Reader物件來將資料讀至 Buffer � Buffered. Reader br = new Buffered. Reader(file. Reader); 使用read. Line()方法來讀入一行字串 n 使用br. close(); 方法來關閉資料流 n

File I/O n 使用File. Writer方法寫入檔案 n 建立欲寫入之檔案 � File file = new File("fiod 6.

File I/O n 使用File. Writer方法寫入檔案 n 建立欲寫入之檔案 � File file = new File("fiod 6. txt"); n 建立File. Reader物件來讀取檔案: � File. Writer file. Writer = new File. Writer(file); n 使用Buffered. Reader物件來將資料讀至 Buffer � Buffered. Writer bw = new Buffered. Writer(file. Writer); 使用bw. write(“A String”)方法來讀入一行字串 n 使用bw. close(); 方法來關閉資料流 n

Other I/O n n 使用Input. Stream. Reader自鍵盤讀取資料 n Buffered. Reader br = new Buffered.

Other I/O n n 使用Input. Stream. Reader自鍵盤讀取資料 n Buffered. Reader br = new Buffered. Reader( new Input. Stream. Reader(System. in)); n 使用br. read. Line(); 方法來取得鍵盤所輸入的字串 n 使用br. close(); 方法來關閉資料流 Output資料至螢幕的方法為我們知之甚捻的 System. out. print(“”);