JAVA IO包的常用類簡單介紹

這幾天,學到了JAVA中的IO包,前幾天就接觸了幾個常用的讀、寫文件的類:

File、RandomAccessFile、OutputStream(InputStream)、Writer(Reader)

一、File類:主要包括以下操作。

1、創建:createNewFile();
2、刪除:delete();
3、判斷是否存在:exists();
4、分隔符與創建目錄:
兩種常用分隔符:
File.separator:在Windows 7中返回“\”,即是路徑中各位置的分隔符
File.pathSeparator:在Windows 7中返回“;”,即各路徑間的分隔符
創建目錄:(new File(…)).mkdir();
5、返回文件長度:length();
6、返回文件列表:
兩種方式:
list():只返回文件名
(File) listFile():返回完整路徑
 

二、各讀寫文件類的讀與寫操作比較

類名稱/操作

方法名

返回值

方法名

RandomAccessFile

writeBytes(String s)

byte

readByte()

 

int

readByte(byte[] b)

writeInt(int v)

int

read()

字節流

輸出流(OutputStream)

輸入流(InputStream)

write(byte[] b)

int(返回讀入個數)

read(byte[] b)

write(byte[],int off, int len )

 

 

write(int b)

int

read()

字符流

Writer

Reader

write(String str)

int

read()

write(cha[] c)

int(返回讀入個數)

read(char[] c)

 

在以上用 read(byte[] b)方法將byte[] 類型的數據輸出時,往往用 String 的構造方法 new String(byte[] b),還可以指定輸出指定範圍的數據:new String(b, 0, len);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章