Java進階篇(五)——Java的I/O技術,java進階
程式中,為了永久的儲存建立的資料,需要將其儲存在磁碟檔案中,以便在其它程式中使用它們。Java的I/O技術可以將資料儲存到文字檔、二進位檔案甚至是ZIP壓縮檔中,以達到永久性儲存資料的要求。
本篇我們要介紹的內容就是Java的I/O技術,即輸入/輸出。
一、輸入/輸出流
流是一組有序的資料序列,根據操作的類型,可分為輸入資料流和輸出資料流。
而專門負責各種方式的輸入、輸出的類,都被放在java.io包中。所有輸入資料流都是抽象類別InputStream(位元組輸入資料流)或抽象類別Reader(字元輸入資料流)的子類;所有輸出資料流都是抽象類別OutputStream(位元組輸出資料流)或抽象類別Writer(字元輸出資料流)的子類。
1. 位元組輸入/輸出流
下面是一個位元組輸入/輸出流的執行個體。其中,第14行為從輸入資料流中讀取位元組並存入buffer數組中,最長讀取2500個位元組,傳回值b為實際讀取的長度。
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 5 public class IOExample { 6 public static void main(String[] args) { 7 try { 8 int b; 9 File file = new File("E:\\test.txt"); // 建立一個檔案10 11 FileInputStream readfile = new FileInputStream(file); // 建立輸入資料流 12 13 byte buffer[] = new byte[2500]; // 建立位元組數組 14 b = readfile.read(buffer, 1, 2000); // 1為buffer數組中的起始位移量 15 String str = new String(buffer, 0, b, "Default"); 16 System.out.println(str); 17 readfile.close(); // 關閉流 18 19 FileOutputStream writefile = new FileOutputStream(file, true); // 建立一個輸出資料流,true為保留原有資料,新資料追加至結尾20 writefile.write(buffer, 0, b); 21 writefile.close(); 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 } 26 }
上述代碼中的FileInputStream類和FileOutputStream類屬於檔案輸入輸出資料流,該類分別繼承自InputStream類和OutputStream類。
InputStream類常用方法需瞭解read()、close()、reset()等方法;OutputStream類常用方法需瞭解write()、flush()、close()等方法。具體可參照API。
2. 字元輸入/輸出流
下面是一個字元輸入/輸出流的執行個體。
1 import java.io.FileReader; 2 import java.io.FileWriter; 3 4 public class IOExample { 5 public static void main(String[] args) { 6 try { 7 FileReader fileread = new FileReader("E:\\test.txt"); // 字元輸入資料流,讀取檔案資訊 8 9 char[] cbuf = new char[500]; 10 int b = fileread.read(cbuf, 0, 500); 11 System.out.println(String.valueOf(cbuf)); 12 fileread.close(); 13 14 FileWriter filewriter = new FileWriter("E:\\test.txt", true); // 字元輸出資料流,向輸出資料流中寫入資訊,並且保留原有資料,追加資料值末尾處 15 filewriter.write(cbuf); 16 filewriter.close(); 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 } 21 }
上述代碼中的FileReader類和FileWriter類也屬於檔案輸入輸出資料流。
3. 檔案輸入/輸出流
上面的代碼中出現了一個新的名詞叫檔案輸入/輸出流,也就是FileInputStream、FileOutputStream和FileReader、FileWriter。它們的作用是與指定的檔案建立串連,將需要的資料永久儲存到檔案中。
FileInputStream類和FileOutputStream類都是用來操作磁碟檔案的,如果使用者的檔案讀取比較簡單,則可使用該類。但由於漢字在檔案中佔用兩個位元組,讀取不好可能出現亂碼現象。
FileReader類和FileWriter類可避免亂碼現象,分別對應了FileInputStream類和FileOutputStream類。
二、File類
File類是io包中唯一代表磁碟檔案本身的對象,可以通過調用File類中的方法,實現建立、刪除、重新命名檔案等。
1. 檔案的建立與刪除
可以使用File類建立一個檔案對象,三種構造方法的文法格式如下:
new File(String pathname)new File(String parent, String child)new File(File f, String child)
pathname是路徑名稱,parent是父路徑字串,child是子路徑字串,f是父路徑對象。如下例:
File file = new File("E:/test.txt");File file = new File("E:/myword","word.txt");
如果E:/myword目錄下沒有名稱為word的檔案,File類對象可通過createNewFile()方法建立一個名稱為word.txt的檔案;如果存在,可通過delete()方法將其刪除。
2. 擷取檔案資訊
File類提供了許多方法用於擷取檔案本身的一些資訊,下表是一些常用方法。
| 方法 |
說明 |
| getName() |
擷取檔案的名稱 |
| canRead() |
判斷檔案是否可讀 |
| canWrite() |
判斷檔案是否可以被寫入 |
| exist() |
判斷檔案是否存在 |
| length() |
擷取檔案的長度 |
| getAbsolutePath() |
擷取檔案的絕對路徑 |
| getParent() |
擷取檔案的父路徑 |
| isFile() |
判斷檔案是否存在 |
| isDirectory() |
判斷檔案是否是一個目錄 |
| isHidden() |
判斷檔案是否是隱藏檔案 |
| lastModifed() |
擷取檔案最後修改時間 |
如我們建立一個檔案,擷取檔案長度等資訊如下:
1 import java.io.File; 2 3 public class FileTest { 4 5 public static void main(String[] args) { 6 File file = new File("E:/myword", "word.txt"); // 建立檔案對象 7 if(file.exists()) { // 判斷檔案是否存在 8 String name = file.getName(); // 擷取檔案名稱 9 long length = file.length(); // 擷取檔案長度10 boolean hidden = file.isHidden(); // 判斷檔案是否是隱藏檔案11 12 System.out.println("檔案名稱:" + name);13 System.out.println("檔案長度是:" + length);14 System.out.println("是否是隱藏檔案:" + hidden);15 } else {16 System.out.println("檔案不存在");17 }18 }19 20 }三、帶緩衝的輸入/輸出流
緩衝是I/O的一種效能最佳化。緩衝流為I/O流增加了記憶體緩衝區。
1. BufferedInputStream類與BufferedOutoutStream類
建構函式如下:
BufferedInputStream(InputStream in)BufferedInputStream(InputStream in, int size)BufferedOutputStream(OutputStream in)BufferedOutputStream(OutputStream in, int size)
2. BufferedReader類與BufferedWriter類
常用方法如下:
| 方法 |
說明 |
| read() |
讀取單個字元 |
| readLine() |
讀取一個文本行,返回為字串;若無資料則返回為null |
| write(String s, int off, int len) |
寫入字串的某一部分 |
| flush() |
重新整理該流的緩衝 |
| newLine() |
寫入一個行分隔字元 |
這裡要注意,在使用BufferedWriter類的write()方法時,資料並沒有立刻被寫入到輸出資料流時,而是首先進入緩衝區中,如果想立刻將緩衝區中的資料寫入輸出資料流中,一定要調用flush()方法。
四、資料輸入/輸出資料流
資料輸入/輸出資料流為DataInputStream類與DateOutputStream類,允許應用程式以與機器無關的方式從底層輸入資料流中讀取基本Java資料類型。
構造方法如下:
DataInputStream(InputStream in)DataOutputStream(OutputStream out)
DataOutputStream類提供了以下三種寫入字串的方法:
writeBytes(String s)writeChars(String s)writeUTF(String s)
五、ZIP壓縮輸入/輸出流
利用ZipOutputStream類對象,可將檔案壓縮為.zip檔案。
建構函式如下:
ZipOutputStream(OutputStream out);
ZipInputStream類可讀取ZIP壓縮格式的檔案,包括對已壓縮和未壓縮條目的支援。
建構函式如下:
ZipInputStream(InputStream out);