JavaSE基礎自學 IO流 File

2021-09-11 10:47:37 字數 4250 閱讀 2131

// 將乙個具體的路徑封裝成file 物件,可以封裝成存在的檔案,也可以不存在的檔案

file f1 =

newfile

("c:"

+ file.separator +

"abc"

+ file.separator +

"1.txt");

//file 已經封裝了分隔符

file f2=

newfile

("c:\\users\\jackchan\\desktop\\gggg");

//file 類常見方法

string file_name = f2.

getname()

;//獲取名字

system.out.

println

(file_name)

;long len = f2.

length()

;//獲取大小

system.out.

println

(len)

; string par = f2.

getparent()

;//獲取父目錄路徑

system.out.

println

(par);--

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

--//獲取路徑

file f1 =

newfile

("abc\\gggg");

string path1 = f1.

getabsolutepath()

;//獲取絕對路徑(具體路徑)

string path2 = f1.

getpath()

;//獲取file封裝的路徑

//獲取檔案最後修改時間

file f2 =

newfile

("d:\\gggg.txt");

long time = f2.

lastmodified()

; date date =

newdate

(time)

; dateformat df = dateformat.

getdatetimeinstance

(dateformat.long, dateformat.long)

; string s1 = df.

format

(date)

; system.out.

println

(s1)

;//建立,刪除檔案

file f3 =

newfile

("c:\\gg.txt");

boolean b = f3.

createnewfile()

;//建立

boolean b1 = f3.

delete()

;//刪除

boolean b2 = f3.

exists()

;//判斷檔案是否存在

boolean b3 = f3.

mkdir()

;//建立目錄

boolean b4 = f3.

mkdirs()

;//建立多級目錄

boolean b5 = f3.

isfile()

;//判斷是否為檔案

boolean b6 = f3.

isdirectory()

;//判斷是否為資料夾

boolean b6 = f3.

ishidden()

;//判斷是否為隱藏檔案

file file =

newfile

("d:\\");

long l = file.

getfreespace()

;//返回分割槽空閒空間

long l1 = file.

gettotalspace()

;//返回分割槽大小

long l2 = file.

getusablespace()

;//返回分割槽空閒空間

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

---

//檔名過濾器

class

filterbyname

implements

filenamefilter

public

boolean

accept

(file dir, string name)

}//檔案過濾器

class

filetor

implements

filefilter

// return pathname.getname().endswith(".class");

}public

class

demo2

}}

//遍歷出所有檔案,使用遞迴

public

static

void

main

(string[

] args)

throws ioexception

//遍歷所有檔案

public

static

void

getall

(file file)

else}*

****

****

****

****

****

****

****

****

****

****

****

****

***//刪除指定資料夾

public

static

void

deletall

(file file)

else

}system.out.

println

(file+

" "+file.

delete()

);}*

****

****

****

****

****

****

****

****

****

****

****

****

****

***//遞迴.

public

static

intxiangjia

(int num)

return num+

xiangjia

(num-1)

;}**

****

****

****

****

****

****

****

****

****

****

****

****

****

***//獲取資料夾中.dll的檔案,存入集合中,利用遞迴

public

static list

get(file file)

public

static

void

select

(file file, list

al, filefilter ff)

else}}

}}class

filetor

implements

filefilter

}

I O基礎流 緩衝流

io流 字元流 轉換流 以下三句話功能相同 1 inputstreamreader isr new inputstreamreader new fileinputstream a.txt 預設字符集。2 inputstreamreader isr new inputstreamreader new ...

IO流基礎使用

io流基礎使用,留著複習 8位1位元組,2個位元組1個字元 位元組流 主要有inputstream outputstream作為基類 字元流 主要有reader writer作為基類 輸入流inputstream int read int read byte b int read byte b,in...

java基礎之io流

1.四大抽象基類 位元組流 inputstream outputstream 字元流 writer reader 位元組輸出流寫檔案用其子類fileoutputstream類 構造 fileoutputstream file file 傳遞file物件包裝檔案 string name 傳遞字串型別檔...