一、Java標准I/O知識體系圖:
二、I/O是什么
I/O 是Input/Output(輸入、輸出)的簡稱,輸入流可以理解為向內存輸入,輸出流是從內存輸出。
三、Java I/O 用途與對應的流一覽
注:粗體為節點流。藍色為轉換流(字節流轉為字符流)。
四、流結構介:
Java所有的流類位於java.io包中,都分別繼承字以下四種抽象流類型。
1.繼承自InputStream/OutputStream的流都是用於向程序中輸入/輸出數據,且數據的單位都是字節(byte=8bit)。
2.繼承自Reader/Writer的流都是用於向程序中輸入/輸出數據,且數據的單位都是字符(2byte=16bit),如圖。
四、流的處理
流分為節點流和處理流兩種:
節點流: 從一個特定的數據源讀寫數據。即節點流是直接操作文件,網絡等的流,例如FileInputStream和FileOutputStream,他們直接從文件中讀取或往文件中寫入字節流。
處理流:是對一個已存在的流的連接和封裝,通過所封裝的流的功能調用實現數據讀寫。如BufferedReader.處理流的構造方法總是要帶一個其他的流對象做參數。一個流對象經過其他流的多次包裝,稱為流的鏈接
五、文件的訪問
(1)讀取文件
如果你需要在不同端使用讀取文件,你可以根據你要讀的文件是二進制文件還是文本文件,或者根據你要處理的數據是准備采取字節方式還是字符方式,決定使用 FileInputStream 或者 FileReader。兩者支持你從文件開頭開始到文件結尾讀取一個字節或者字符,也可以將讀取的多個字節或字符,寫入到內存的字節數組或字符數組。
單字節讀取文件示例:
public class test5 { public static void main(String[] args) throws Exception { String filepath = "test.bin"; java.io.InputStream is = null; try { is = new FileInputStream(filepath); int data = -1; while ((data = is.read()) != -1) {// -1 表示讀取到達文件結尾 //操作數據 System.out.print((byte)data + " "); } } finally { if (is != null) { is.close();// 關閉流 } } } }
字節數組讀取文件示例:
public class test5 { public static void main(String[] args) throws Exception { String filepath = "test.bin"; java.io.InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(filepath));// 組裝BufferedInputStream流,加入緩沖能力 byte[] data = new byte[256]; int len = -1; while ((len = is.read(data)) != -1) {// -1 表示讀取到達文件結尾 //操作數據 for (int i = 0; i < len; i++) { System.out.print(data[i] + " "); } } } finally { if (is != null) { is.close();// 關閉流 } } } }
單字符讀取文件示例:
public class test5 { public static void main(String[] args) throws Exception { String filepath = "test.txt"; java.io.Reader r = null; try { r = new FileReader(filepath); int data = -1; while ((data = r.read()) != -1) {// -1 表示讀取到達文件結尾 //操作數據 System.out.print((char) data); } } finally { if (r != null) { r.close();// 關閉流 } } } }
字符數組讀取文件示例:
public class test5 { public static void main(String[] args) throws Exception { String filepath = "file.txt"; java.io.Reader r = null; try { r = new BufferedReader(new FileReader(filepath));// 組裝BufferedReader流,加入緩沖能力 char[] data = new char[256]; int len = -1; while ((len = r.read(data)) != -1) {// -1 表示讀取到達文件結尾 //操作數據 for (int i = 0; i < len; i++) { System.out.print(data[i]); } } } finally { if (r != null) { r.close();// 關閉流 } } } }
public class test5 { public static void main(String[] args) throws Exception { String filepath = "file.txt"; java.io.Reader r = null; try { r = new BufferedReader(new FileReader(filepath));// 組裝BufferedReader流,加入緩沖能力 char[] data = new char[256]; int len = -1; while ((len = r.read(data)) != -1) {// -1 表示讀取到達文件結尾 //操作數據 for (int i = 0; i < len; i++) { System.out.print(data[i]); } } } finally { if (r != null) { r.close();// 關閉流 } } } }
(2)寫入文件與讀取文件類似
單字節寫入文件示例:
public class test5 { public static void main(String[] args) throws Exception { String filepath = "test.bin"; java.io.OutputStream os = null; try { os = new FileOutputStream(filepath); os.write('1'); os.write('2'); os.write('3'); os.write('4'); os.flush();// 把緩沖區內的數據刷新到磁盤 } finally { if (os != null) { os.close();// 關閉流 } } } }
字符數組寫入文件示例:
public class test5 { public static void main(String[] args) throws Exception { String filepath = "file.txt"; java.io.Writer w = null; try { w = new BufferedWriter(new FileWriter(filepath));// 組裝BufferedWriter流,加入緩沖能力 // 模擬 char[] data = new char[256]; String f = "0123456789abcdefghijklmnopqrstuvwxyz"; Random rd = new Random(); for (int i = 0; i < data.length; i++) { data[i] = f.charAt(rd.nextInt(f.length())); } w.write(data); w.flush();// 把緩沖區內的數據刷新到磁盤 } finally { if (w != null) { w.close();// 關閉流 } } } }
(3)隨機訪問文件
如果你需要不按特定的存取順序,隨意讀取或者寫入文件,可以考慮RandomAccessFile。
void seek(long pos) 設置到此文件開頭測量到的文件指針偏移量,在該位置發生下一個讀取或寫入操作。
public class test5 { public static void main(String[] args) throws Exception { RandomAccessFile file = null; try { file = new RandomAccessFile("test.bin", "rw"); file.seek(0); file.writeChar('1'); file.seek(0); System.out.println(file.readChar()); /** * 讀取 */ int data = -1; while ((data = file.read()) != -1) {// -1 表示讀取到達文件結尾 //操作數據 System.out.print((byte)data + " "); } } finally { if (file != null) { file.close();// 關閉流 } } } }