IO流
- IO流基礎
- 位元組流
- 字符流
- Properties
- 字符通向位元組
- 物件的流
- 列印流
IO流基礎
| 表格 | 輸入流 | 輸出流 |
|---|---|---|
| 位元組流 | InputStream | OutputStream |
| 字符流 | Reader | Writer |
位元組流
所有都需要拋出例外
OutputStream:抽象類,表示輸出位元組的所有的超類,
FileOutputStream把記憶體中的資料寫入到硬碟的檔案當中,
FileOutputStream(String name);//創建一個向具有名稱的檔案中寫入資料的輸出檔案流
FileOutputStream(File file);
FileOutputStream(String name,boolean append);//true 追加資料
//false 覆寫原檔案
FileOutputStream(File file,boolean append);//
close();//關閉此輸出流并釋放
flush();//重繪
write(byte[] b);//將b.length位元組從指定的位元組陣列寫入此輸出流
//b陣列中,第一個位元組是正數,查詢ASCII表
//第一個位元組是負數,第一個位元組會和第二個位元組組成一個中文顯示,查詢GBK
write(byte[] b,int off,int len);//從指定的位元組陣列寫入len位元組,off開始
write(int b);//將指定的位元組輸出流
換行符號:
windows:\r\n
linux:/n
mac:/r
InputStream:抽象類,表示輸入位元組的所有的超類,
FileInputStream把硬碟檔案中的資料,讀到記憶體中使用,
FileInputStream(String name);//讀取
FileInputStream(File file);
read();//從輸入流中讀取資料的一個位元組并回傳,讀到檔案的末尾回傳-1
read(byte[] b);//從輸入流中讀取一定數量的位元組,并將其存盤在緩沖區陣列b中
//回傳值int每次讀取的有效位元組個數
close();
BufferedOutputStream:位元組緩沖輸出流
BufferedOutputStream(OutputStream out);
BufferedOutputStream(OutputStream out,int size);
close();
flush();
write(byte[] b);//將b.length位元組從指定的位元組陣列寫入此輸出流
write(byte[] b,int off, int len);
write(int b);
BufferedInputStream:位元組緩沖輸入流
BufferedInputStream(InputStream in);
BufferedInputStream(InputStream in,int size);
read();
read(byte[] b);
close();
字符流
FileReader:檔案字符輸入流
可以讀取IDE默認編碼格式(UTF-8)的檔案,讀取系統默認編碼(中文GBK)會產生亂碼
FileReader(String name);
FileReader(File file);
read(char[] cs);//一次讀取多個字符
FileWriter:檔案字符輸出流
FileWriter(String name);//構造
FileWriter(File file);
FileWriter(String fileName,boolean append);
FileWriter(File file,boolean append);
write(int c);
write(char[] sc);
write(char[] cs,int off,int len);
write(String str);
write(String str,int off,int len);
flush();
close();//關閉則不能繼續寫入資料
BufferedWriter:字符緩沖輸出流
BufferedWriter(Writer out);
BufferedWriter(Writer out,int size);
write(int c);
write(char[] ch);
write(char[] ch,int off, int len);
write(String str);
write(String str,int off, int len);
flush();
closr();
newLine();//特有的成員方法,根據不同的作業系統,獲取不同的行分割符
BufferedReader:字符緩沖流輸入流
BufferedReader(Reader in);
BufferedReader(Reader in,int size);
read();
read(char[] ch);
close();
readLine();//特有的成員方法,讀取一行資料//可以使用while回圈,直至讀取到null
Properties
Properties:可保存在流中,或從流中加載(雙列集合,默認ket,value)
setProperty(String key,String value);//呼叫Hashtable的方法put
getProperty(String key);//通過key找到value值,此方法相當于Map集合中的get(key)方法
stringPropertyNames();//回傳的是Set集合鍵集
store(OutputStream out,String comments);//OutputStream位元組輸出流,不可寫入中文
store(Writer writer,String comments);//Writer字符輸出流,可以寫入中文
//String comments注釋,不能使用中文,默認是Unicode編碼,一般使用空字串""
load(InputStream inStream);
load(Reader reader);
字符通向位元組
InputStreamReader:是字符流通向位元組流的橋梁
InputStreamReader(InputStream in);
InputStreamReader(InputStream in,String charsetName);//指定編碼標的名稱要和檔案相同,否則會出現亂碼
read();
read(char ch);
close();
OutputStreamWriter:是字符流通向位元組流的橋梁
OutputStreamWriter(OutputStream out);
OutputStreamWriter(OutputStream out,String charsetNamwe);//String charsetNamwe指定的編碼表名稱,不指定默認使用UTF-8
write(int c);
write(char[] ch);
write(char[] ch,int off,int len);
write(String str);
write(String str,int off,int len);
flush();
close();
物件的流
ObjectOutputStream:物件的序列化流,把物件以流的方式寫入到檔案中保存
ObjectOutputStream(OutputStream out);
writeObject(Object obj);
實作序列化和反序列化時,類必須實作Serializable介面,沒有則會拋出NotSerialzableException例外
ObjectInputStream:物件的反序列化流,把檔案中的物件,以流的方式讀取出來
ObjectInputStream(InputStream in);
readObject();//特有的成員方法,從 ObjectInputStream讀取該物件
//方法宣告拋出了classNotFoundException,檔案不存在時拋出
被static修飾的成員變數不能被序列化的,序列化的都是物件
transient瞬態關鍵字,不能被序列化
若是進行輸出后,改動類中的關鍵字,需要固定版本號,
private static final long serialVersionUID=1L;
列印流
PrintStream:列印流
PrintStream特點:只負責資料的輸出,不會拋出IOException
可以改變輸出陳述句的目的地;輸出陳述句默認在控制臺輸出
PrintStream(File file);
PrintStream(OutputStream out);
PrintStream(String fileName);
static void setOut(PrintStream out);
print(任意型別的值);//特有方法,寫的資料照樣輸出97->97
println(任意型別的值并換行);//
close();
flush();
write(byte[] b);//將b.length位元組從指定的位元組陣列寫入此輸出流
write(byte[] b,int off, int len);
write(int b);//97->a
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/272646.html
標籤:java
