一、IO流的三種分類方式
1.按流的方向分為:輸入流和輸出流
2.按流的數(shù)據(jù)單位不同分為:字節(jié)流和字符流
3.按流的功能不同分為:節(jié)點流和處理流
二、IO流的四大抽象類:
字符流:Reader Writer
字節(jié)流:InputStream(讀數(shù)據(jù))
OutputStream(寫數(shù)據(jù))
三、InputStream的基本方法
int read() throws IOException 讀取一個字節(jié)以整數(shù)形式返回,如果返回-1已到輸入流的末尾
void close() throws IOException 關閉流釋放內(nèi)存資源
long skip(long n) throws IOException 跳過n個字節(jié)不讀
四、OutputStream的基本方法
void write(int b) throws IOException 向輸出流寫入一個字節(jié)數(shù)據(jù)
void flush() throws IOException 將輸出流中緩沖的數(shù)據(jù)全部寫出到目的地
五、Writer的基本方法
void write(int c) throws IOException 向輸出流寫入一個字符數(shù)據(jù)
void write(String str) throws IOException將一個字符串中的字符寫入到輸出流
void write(String str,int offset,int length)
將一個字符串從offset開始的length個字符寫入到輸出流
void flush() throws IOException
將輸出流中緩沖的數(shù)據(jù)全部寫出到目的地
六、Reader的基本方法
int read() throws IOException 讀取一個字符以整數(shù)形式返回,如果返回-1已到輸入流的末尾
七、節(jié)點流類型
八、訪問文件之FileInputStream和FileOutputStream繼承基類用于向文件中輸入輸出字節(jié)
九、訪問文件之FileReader和FileWriter繼承基類用于向文件中輸入輸出字符
----輸出流在構(gòu)造函數(shù)第二個參數(shù)可以設置true意義為跟在已有文件后進行輸入
----此類流會拋出FileNotFoundException需要對其進行顯示捕捉
十、緩沖流:緩沖流要套接在相應的節(jié)點流之上,提高了讀寫的效率。
此處理流的構(gòu)造方法都得傳相對應的基類類型
BufferedReader:提供了readLine方法用于高校讀取一行字符串
BufferedWriter:提供了newLine用于寫入一個行分隔符也就是換行
BufferedInputStream 沒多大用處
BufferedOutputStream 沒多大用處
十一、轉(zhuǎn)換流:主要作用將字節(jié)流轉(zhuǎn)換成字符流。用處較大!
轉(zhuǎn)換流在構(gòu)造時可以指定其編碼集合
InputStreamReader需要和InputStream套接
OutputStreamWriter需要和OutputStream套接
例:OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream(文件路徑);
方法例:osw.getEncoding(); 獲得流的編碼方式
十二、數(shù)據(jù)流與字節(jié)數(shù)組流:
數(shù)據(jù)流主要為實現(xiàn)可以存取Java原始數(shù)據(jù)類型如long,boolean
數(shù)據(jù)流是字節(jié)流
DataInputStream需要和InputStream套接
DataOutputStream需要和OutputStream套接
DataInputStream方法:readBoolean() readInt() read……()……
readUTF():網(wǎng)絡傳輸常用方法 讀一個Unicode字符串
DataOutputStream方法與DataInputStream基本對應為寫的方法
//此構(gòu)造函數(shù)等于已可以往一個字節(jié)數(shù)組里輸入內(nèi)容
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
//此方法為獲取一個字節(jié)數(shù)組方法返回字節(jié)數(shù)組
baos.toByteArray();
//此方法獲取字節(jié)數(shù)組占了多少字節(jié)
new ByteArrayInputStream(一個字節(jié)數(shù)組)。available()
1ByteArrayOutputStream baos =
2 new ByteArrayOutputStream();
3 DataOutputStream dos =
4 new DataOutputStream(baos);
5 try {
6 dos.writeDouble(Math.random());
7 dos.writeBoolean(true);
8 ByteArrayInputStream bais =
9 new ByteArrayInputStream(baos.toByteArray());
10 System.out.println(bais.available());
11 DataInputStream dis = new DataInputStream(bais);
12 System.out.println(dis.readDouble());
13 System.out.println(dis.readBoolean());
14 dos.close(); dis.close();
15 } catch (IOException e) {
16 e.printStackTrace();
17 }
十二、Print流
Print流只有輸出流無輸入流,PrintWriter和PrintStream分別針對字符字節(jié)
兩個類提供了重載的Print和Println方法用于多種數(shù)據(jù)類型的輸出
PrintWriter和PrintStream的輸出操作不會拋出異常
PrintWriter和PrintStream有自動flush功能
----System.setOut(接收OutputStream類):用于設置系統(tǒng)默認輸出流
十三、Object流
等同于c#序列化,用直接將Object寫入或讀出
transient關鍵字為不序列化此成員變量
需要序列化的類必須實現(xiàn)Serializable接口
主要方法:writeObject(Object); readObject();
讀出為Object類型需要強轉(zhuǎn)數(shù)據(jù)類型
1 import java.io.*;
2
3 public class TestObjectIO {
4 public static void main(String args[]) throws Exception {
5 T t = new T();
6 t.k = 8;
7 FileOutputStream fos = new FileOutputStream("d:/share/java/io/testobjectio.dat");
8 ObjectOutputStream oos = new ObjectOutputStream(fos);
9 oos.writeObject(t);
10 oos.flush();
11 oos.close();
12
13 FileInputStream fis = new FileInputStream("d:/share/java/io/testobjectio.dat");
14 ObjectInputStream ois = new ObjectInputStream(fis);
15 T tReaded = (T)ois.readObject();
16 System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k);
17
18 }
19 }
20
21 class T
22 implements Serializable
23 {
24 int i = 10;
25 int j = 9;
26 double d = 2.3;
27 transient int k = 15;
28 }
1.按流的方向分為:輸入流和輸出流
2.按流的數(shù)據(jù)單位不同分為:字節(jié)流和字符流
3.按流的功能不同分為:節(jié)點流和處理流
二、IO流的四大抽象類:
字符流:Reader Writer
字節(jié)流:InputStream(讀數(shù)據(jù))
OutputStream(寫數(shù)據(jù))
三、InputStream的基本方法
int read() throws IOException 讀取一個字節(jié)以整數(shù)形式返回,如果返回-1已到輸入流的末尾
void close() throws IOException 關閉流釋放內(nèi)存資源
long skip(long n) throws IOException 跳過n個字節(jié)不讀
四、OutputStream的基本方法
void write(int b) throws IOException 向輸出流寫入一個字節(jié)數(shù)據(jù)
void flush() throws IOException 將輸出流中緩沖的數(shù)據(jù)全部寫出到目的地
五、Writer的基本方法
void write(int c) throws IOException 向輸出流寫入一個字符數(shù)據(jù)
void write(String str) throws IOException將一個字符串中的字符寫入到輸出流
void write(String str,int offset,int length)
將一個字符串從offset開始的length個字符寫入到輸出流
void flush() throws IOException
將輸出流中緩沖的數(shù)據(jù)全部寫出到目的地
六、Reader的基本方法
int read() throws IOException 讀取一個字符以整數(shù)形式返回,如果返回-1已到輸入流的末尾
七、節(jié)點流類型
八、訪問文件之FileInputStream和FileOutputStream繼承基類用于向文件中輸入輸出字節(jié)
九、訪問文件之FileReader和FileWriter繼承基類用于向文件中輸入輸出字符
----輸出流在構(gòu)造函數(shù)第二個參數(shù)可以設置true意義為跟在已有文件后進行輸入
----此類流會拋出FileNotFoundException需要對其進行顯示捕捉
十、緩沖流:緩沖流要套接在相應的節(jié)點流之上,提高了讀寫的效率。
此處理流的構(gòu)造方法都得傳相對應的基類類型
BufferedReader:提供了readLine方法用于高校讀取一行字符串
BufferedWriter:提供了newLine用于寫入一個行分隔符也就是換行
BufferedInputStream 沒多大用處
BufferedOutputStream 沒多大用處
十一、轉(zhuǎn)換流:主要作用將字節(jié)流轉(zhuǎn)換成字符流。用處較大!
轉(zhuǎn)換流在構(gòu)造時可以指定其編碼集合
InputStreamReader需要和InputStream套接
OutputStreamWriter需要和OutputStream套接
例:OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream(文件路徑);
方法例:osw.getEncoding(); 獲得流的編碼方式
十二、數(shù)據(jù)流與字節(jié)數(shù)組流:
數(shù)據(jù)流主要為實現(xiàn)可以存取Java原始數(shù)據(jù)類型如long,boolean
數(shù)據(jù)流是字節(jié)流
DataInputStream需要和InputStream套接
DataOutputStream需要和OutputStream套接
DataInputStream方法:readBoolean() readInt() read……()……
readUTF():網(wǎng)絡傳輸常用方法 讀一個Unicode字符串
DataOutputStream方法與DataInputStream基本對應為寫的方法
//此構(gòu)造函數(shù)等于已可以往一個字節(jié)數(shù)組里輸入內(nèi)容
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
//此方法為獲取一個字節(jié)數(shù)組方法返回字節(jié)數(shù)組
baos.toByteArray();
//此方法獲取字節(jié)數(shù)組占了多少字節(jié)
new ByteArrayInputStream(一個字節(jié)數(shù)組)。available()
1ByteArrayOutputStream baos =
2 new ByteArrayOutputStream();
3 DataOutputStream dos =
4 new DataOutputStream(baos);
5 try {
6 dos.writeDouble(Math.random());
7 dos.writeBoolean(true);
8 ByteArrayInputStream bais =
9 new ByteArrayInputStream(baos.toByteArray());
10 System.out.println(bais.available());
11 DataInputStream dis = new DataInputStream(bais);
12 System.out.println(dis.readDouble());
13 System.out.println(dis.readBoolean());
14 dos.close(); dis.close();
15 } catch (IOException e) {
16 e.printStackTrace();
17 }
十二、Print流
Print流只有輸出流無輸入流,PrintWriter和PrintStream分別針對字符字節(jié)
兩個類提供了重載的Print和Println方法用于多種數(shù)據(jù)類型的輸出
PrintWriter和PrintStream的輸出操作不會拋出異常
PrintWriter和PrintStream有自動flush功能
----System.setOut(接收OutputStream類):用于設置系統(tǒng)默認輸出流
十三、Object流
等同于c#序列化,用直接將Object寫入或讀出
transient關鍵字為不序列化此成員變量
需要序列化的類必須實現(xiàn)Serializable接口
主要方法:writeObject(Object); readObject();
讀出為Object類型需要強轉(zhuǎn)數(shù)據(jù)類型
1 import java.io.*;
2
3 public class TestObjectIO {
4 public static void main(String args[]) throws Exception {
5 T t = new T();
6 t.k = 8;
7 FileOutputStream fos = new FileOutputStream("d:/share/java/io/testobjectio.dat");
8 ObjectOutputStream oos = new ObjectOutputStream(fos);
9 oos.writeObject(t);
10 oos.flush();
11 oos.close();
12
13 FileInputStream fis = new FileInputStream("d:/share/java/io/testobjectio.dat");
14 ObjectInputStream ois = new ObjectInputStream(fis);
15 T tReaded = (T)ois.readObject();
16 System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k);
17
18 }
19 }
20
21 class T
22 implements Serializable
23 {
24 int i = 10;
25 int j = 9;
26 double d = 2.3;
27 transient int k = 15;
28 }

