Java——>IO__Java

來源:互聯網
上載者:User
io

io
Input / Output
輸入 / 輸出

java.io 包

File
RandomAccessFile

InputStream / OutputStream 位元組流的抽象父類
FileInputStream / FileOutputStream 插檔案,直接讀寫檔案資料
BufferedInputStream / BufferedOutputStream 記憶體緩衝,提高單位元組讀寫效率
DataInputStream / DataOutputStream 固定位元組格式 writeInt() writeDouble() writeUTF()
PrintStream 任何資料轉成字串輸出 print() println()
ByteArrayInputStream / ByteArrayOutputStream 與記憶體中 byte[] 數組相接 toByteArray()
ObjectInputStream / ObjectOutputStream 序列化、還原序列化 writeObject() readObject()

Reader / Writer 字元流的抽象父類
InputStreamReader / OutputStreamWriter 編碼轉換流
FileReader / FileWriter 內部是“檔案位元組流”接“編碼轉換流”
BufferedReader / BufferedWriter 字元緩衝,readLine()
PrintWriter 與 PrintStream 相同 File 封裝磁碟路徑字串

可以封裝
*)檔案路徑
*)目錄路徑
*)不存在的路徑

建立對象

1.File f = new File(“d:\abc\a.txt”);

2.File f = new File(“d:\abc\”, “a.txt”);

方法

*) 檔案或目錄的屬性

canRead()         canWrite()         canExecute()                 是否可讀可寫可執行        isHidden()                  是否隱藏檔案        exists()                        表示的路徑是否存在        getAbsolutePath()   完整路徑        getName()           檔案名稱        getParent()         父目錄        lastModified()  最後修改時間,毫秒值        length()                檔案位元組量,對目錄無效        isDirectory()       是否檔案夾        isFile()                是否檔案        getTotalSpace() 空間總大小        getFreeSpace()  可用空間

*) 檔案或目錄操作

        createNewFile() 建立檔案        delete()                刪除檔案或“空目錄”        mkdir()                 建立單層目錄        mkdirs()                建立多層目錄        renameTo()          改名、移動        *) 建立刪除重新命名方法返回 boolean,表示操作是否成功        File.createTempFile(首碼,  尾碼)        在系統臨時目錄建立臨時檔案

*) 檔案清單

        list()          返回 String[],包含子檔案、子目錄名稱        listFiles() 返回 File[],包含子檔案、子目錄的 File 對象        list(FilenameFilter)        listFiles(FilenameFilter)        listFiles(FileFilter)                                只列出符合過濾條件的檔案、目錄,                                參數: 外接的過濾器
RandomAccessFile 讀寫檔案的工具

檔案當做位元組數組,
用下標讀寫指定位置的位元組值

建立對象

RandomAccessFile raf =
new RandomAccessFile(檔案, “r”);

r - 唯讀

RandomAccessFile raf =
new RandomAccessFile(檔案, “rw”);

rw - 讀寫

方法

write(int b)
int 四個位元組中,只輸出末尾一個位元組值,
[1][2][3][4] –> [4]

write(byte[] buf)
輸出數組中全部位元組值

write(byte[] buf, int from, int length)
輸出數組中從 from 開的 length 個位元組值

read()
讀取一個位元組,再補3個0位元組,轉成int
[4] –> [1][2][3][4]

讀取結束,再讀取,返回 -1

read(byte[] buf)
根據數組長度,讀取一批位元組值,放入數組,
返回這一批的位元組數量

讀取結束,再讀取,返回 -1

seek(int position)
定位下標到指定的位置

getFilePointer()
獲得當前下標位置

writeInt(int i) 完整輸出 int 的4個位元組
writeDouble(double d) 完整輸出 double 的8個位元組
…
writeUTF(String s)
先輸出兩個位元組表示字串的位元組長度
再輸出字串的位元組值

readInt() 讀取4個位元組值轉成int
readDouble()讀取8個位元組值轉成double
…
readUTF()
先讀取2個位元組值,來確定字串的位元組長度,
再讀取這些位元組值轉成 String

*)讀取結束再讀取,會出現 EOFException

EOF - END OF FILE
流 Stream 資料讀寫操作,抽象成資料在管道中流動

流是單向的

*)輸入資料流
*)輸出資料流

流過去的資料,不能重複流動,
只能從頭到尾順序讀寫資料 InputStream / OutputStream

位元組流抽象父類

方法 OutputStream

write(int b)
write(byte[] buf)
write(byte[] buf, int from, int length)
flush() 刷出快取資料 InputStream

read()
read(byte[] buf)
available() 獲得剩餘的可讀取位元組量 FileInputStream / FileOutputStream

直接與檔案相接,讀寫檔案中的資料

建立對象

FileOutputStream in =
new FileOutputStream(檔案);

不管檔案是否存在,
都建立新的空檔案

FileOutputStream in =
new FileOutputStream(檔案, true);

不存在,建立;
存在,追加。 進階流、操作流 與其他流相接,提供特定的資料處理功能 對進階流的操作,會對相接的流執行相同操作 BufferedInputStream / BufferedOutputStream

提供記憶體緩衝區,提高單位元組讀寫效率

建立對象

1.BufferedOutputStream out =
new BufferedOutputStream(相接的流);

內部緩衝數組容量 8192

2.BufferedOutputStream out =
new BufferedOutputStream(相接的流, 16*1024);

內部緩衝數組容量 16k
DataInputStream / DataOutputStream

讀寫固定位元組格式的資料

建立對象

DataOutputStream out =
new DataOutputStream(相接的流);

方法 DataOutputStream

writeInt(int i)
writeDouble(double d)
…
writeUTF(String s) DataInputStream

readInt()
readDouble()
…
readUTF() PrintStream

任何類型資料轉成字串輸出

write(97)       61writeInt(97)    00 00 00 61print(97)       39 37      "97"      '9'  '7'  

建立對象

PrintStream out = new PrintStream(相接的流);

PrintStream out = new PrintStream(檔案);

PrintStream out = new PrintStream(檔案, 字元編碼);

方法

print()
println()

字元編碼 Encoding 字元集 Charset

asc-ii

  0 - 127

iso-8859-1
Latin-1

  160 - 255  西歐字元

GB2312 雙位元組
7+ 中文字元 喆,鎔

GBK
20902 中文

BIG5
jpxxx
krxxx
alxxx

Unicode

*) 統一碼、萬國碼*) 70萬+*) 常用字元表,雙位元組*) 生僻字元表,三位元組或以上*) java 的 char 類型採用 Unicode 編碼*) UTF-8  Unicde傳輸格式    *)英文,單位元組    *)某些字元,雙位元組    *)中文,三位元組

java 的字元編碼轉換運算

*)Unicode –> 其他編碼

  String s = "abc中文";  //轉成預設編碼  byte[] a = s.getBytes();  //轉成 UTF-8 編碼  byte[] a = s.getBytes("UTF-8");

*)其他編碼 –> Unicode

  //從預設編碼轉換  String s = new String(byte[]數組);  //從指定的編碼轉換  String s = new String(byte[]數組, "UTF-8");
Reader / Writer

字元流的抽象父類

方法 Writer

write(int c)
int末尾兩個位元組是一個 char 類型字元,
輸出末尾的兩個位元組值

write(char[] buf)
write(char[] buf, int from, int length)
write(String s) Reader

read()
讀取 char 類型字元,
再補兩個 0 位元組轉成 int

read(char[] buf) InputStreamReader / OutputStreamWriter

字元編碼轉換流

OutputStreamWriter
將 Unicode 字元轉換成其他編碼位元組值輸出

InputStreamReader
將讀取的其他編碼字元,轉成 Unicode 編碼字元

建立對象

OutputStreamWriter out =
new OutputStreamWriter(相接的位元組流);

OutputStreamWriter out =
new OutputStreamWriter(相接的位元組流, 字元編碼); FileReader / FileWriter 它內部是“檔案位元組流”接“編碼轉換流”

不能指定字元編碼,只能使用預設編碼

建立對象

FileWriter out = new FileWriter(檔案);

等價於:
OutputStreamWriter out =
new OutputStreamWriter(
new FileOutputStream(檔案)); BufferedReader / BufferedWriter

字元緩衝流

建立對象

BufferedWriter out =
new BufferedWriter(相接的字元流);

BufferedReader 方法

readLine()
讀取一行字串,不包含末尾分行符號

讀取結束再讀取,會返回 null PrintWriter 與 PrintSream 相同 PrintWriter 可以接字元流,也可以接位元組流

PrintStream 接位元組流

建立對象

1.PrintWriter out = new PrintWriter(相接的字元流);
2.PrintWriter out = new PrintWriter(相接的位元組流);
3.PrintWriter out = new PrintWriter(檔案);
3.PrintWriter out = new PrintWriter(檔案, 編碼); ObjectInputStream / ObjectOutputStream 序列化、還原序列化 ObjectOutputStream
將對象轉成一串位元組序列輸出

ObjectInputStream
讀取序列化資料,恢複對象

不序列化的成員
*)static 屬於類,不會隨對象被序列化輸出
*)transient 臨時,
只在程式運行期間,在記憶體中存在
不會被序列化持久儲存

序列化頭部位元組值

*)建立 ObjectOutputStream 對象時,
會自動輸出 4 個位元組頭部值
*)建立 ObjectInputStream 對象時,
會自動讀取 4 個頭部位元組值,
來確認,後面的資料是對象序列化資料

序列化版本號碼

static final long serialVersionUID

*)控制舊版本的資料,
不允許恢複成新版本的類型

建立對象

ObjectOutputStream out =
new ObjectOutputStream(相接的流);

方法 ObjectOutputStream

writeObject(Object obj)
序列化輸出 ObjectInputStream

readObject()
還原序列化讀取 ByteArrayInputStream / ByteArrayOutputStream

與記憶體中 byte[] 數組相接,
直接讀寫數組中的資料

建立對象

ByteArrayInputStream in =
new ByteArrayInputStream(byte[]數組);

ByteArrayOutputStream out =
new ByteArrayOutputStream();

  內部有一個數組,輸出的資料,  放在它內部數組中,  輸出結束後,可以取出內部數組

方法 ByteArrayOutputStream

toByteArray()
取出內部的數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.