亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

Flie類

1.什么是File類

File類就是當(dāng)前系統(tǒng)中,文件或文件夾的抽象表示。

通過使用File類的實(shí)例對象,我們就可以通過代碼實(shí)現(xiàn)計(jì)算機(jī)的文件控制,即文件的增刪改查等操作。

2.File類的使用

首先為了保證其他文件的安全性,建議先建立一個(gè)Test文件夾專門用于File對象的聯(lián)系。

public static void main(String[] args) throws IOException {

File f1 = new File("D:/AAAkejian/作業(yè)/test");

File f2 = new File("D:\AAAkejian\作業(yè)\test1");

f1.mkdir();

f2.mkdir();

f1 = new File("D:/AAAkejian/作業(yè)/test/test.txt");

f1.createNewFile();

File f3 = new File("D:"+File.separator+

"AAAkejian"+File.separator+

"作業(yè)"+File.separator+"test1");

}

其中:

D:/AAAkejian/作業(yè)/test,代表路徑名,“ / ”和“”都表示路徑分隔符;

在windows系統(tǒng)下,兩種分隔符都支持,在linux和mac系統(tǒng)下只支持“ / ”

使用File.separator,表示動(dòng)態(tài)分隔符。但在日常開發(fā)中,由于windows系統(tǒng)中兩者都支持,而且在使用“ ”時(shí),還需要考慮轉(zhuǎn)義字符,因此我們常用的路徑分隔符為“ / ”;

3.File對象的使用

1)創(chuàng)建目錄或文件

File f1 = new File("D:/AAAkejian/作業(yè)/test");

//創(chuàng)建目錄

f1.mkdir();

f1 = new File("D:/AAAkejian/作業(yè)/test/test.txt");

//創(chuàng)建文件

f1.createNewFile();

f1 = new File("D:/AAAkejian/作業(yè)/test/test2");

//創(chuàng)建多級(jí)目錄

f1.mkdirs();

2)刪除目錄或文件

//刪除目錄或文件

f1.delete();

//當(dāng)程序結(jié)束時(shí)再進(jìn)行刪除

f2.deleteOnExit();

3)修改目錄或文件

IO流

1.什么是IO流

IO分別是兩個(gè)單詞的首字母縮寫,I:Inputstream輸入流 O:Outputstream輸出流

2.IO流的作用:對文件中的內(nèi)容進(jìn)行操作;

輸入:讀操作(讀取文件的內(nèi)容) 輸出:寫操作(向文件中寫內(nèi)容)

這里的輸入輸出是針對JAVA程序而言,從文件中讀取內(nèi)容到Java程序即為輸入;

同理,從Java程序向文件中寫入內(nèi)容就叫輸出。

3.IO流的方向

1)根據(jù)流的方向

輸入流:程序可以從中讀取數(shù)據(jù)的流

輸出流:程序可以從中寫入數(shù)據(jù)的流

2)根據(jù)流的單位

字節(jié)流:以字節(jié)為單位傳輸數(shù)據(jù)的流

字符流:以字符為單位傳輸數(shù)據(jù)的流

3)根據(jù)功能

節(jié)點(diǎn)流:直接和文件進(jìn)行交互

處理流:不直接作用在文件上

IO流中有(字節(jié)輸入/輸出流、字符輸入/輸出流)四個(gè)基本流,其他的流都是再這四個(gè)流的基礎(chǔ)上進(jìn)行拓展的

4.Writer字符輸出流

1)Writer類是所有字符輸出流的跟類

2)使用Writer向文件中添加內(nèi)容

public static void main(String[] args) throws IOException {

Writer w1 = new FileWriter("D:/AAAkejian/WriterTest/test2.txt");

String str = "這是第二遍練習(xí)";

w1.write(str);

//該方式則可以進(jìn)行添加而不覆蓋

Writer w2 = new FileWriter("D:/AAAkejian/WriterTest/test2.txt",true);

str = "這是添加的內(nèi)容";

w2.write(str);

//Append也可用于添加

w1.append(str);

//刷新流

w1.flush();

//關(guān)閉流

w1.close();

w2.flush();

w2.close();

}

3)使用Writer直接向文件內(nèi)添加內(nèi)容時(shí),后添加的會(huì)覆蓋先添加的內(nèi)容,那么在進(jìn)行內(nèi)容追加時(shí)就需要添加上一個(gè)true,表示允許追加內(nèi)容到文件中。見上圖

5.Reader字符輸入流

1)Reader類是所有字符輸入流的跟類

2)使用FileReader實(shí)現(xiàn)類進(jìn)行文件的讀取操作:

public static void main(String[] args) throws IOException {

Reader reader = new FileReader("D:/AAAkejian/ReaderTest/test1.txt");

int count = 0;

char[] cList = new char[10];

while( (count=reader.read(cList)) !=-1 ){

String str=new String(cList,0,count);

System.out.print(str);

}

}

6,結(jié)合輸入輸出流可以實(shí)現(xiàn)文件的復(fù)制功能

public void test1() throws IOException {

//創(chuàng)建字符輸入流

FileReader fr = new FileReader("D:/AAAkejian/Test/test1.txt");

//創(chuàng)建字符輸出流

FileWriter fw = new FileWriter("D:/AAAkejian/Test/test2.txt");

//記錄讀取到的個(gè)數(shù)

int count = 0;

//每次讀取的內(nèi)容放入這個(gè)數(shù)組中

char[] cList = new char[10];

while ((count = fr.read(cList))!=-1){

fw.write(cList,0,count);

fw.flush();

}

fw.close();

fr.close();

}

這里需要注意的是,字符流只適用于文本的輸入輸出,對于圖片,視頻等二進(jìn)制文件則無法使用字符流進(jìn)行操作。

7.字節(jié)流

1)字節(jié)輸出流——OutputStream(所有字節(jié)輸出流的父類)

字節(jié)輸出流的使用,以FileOutputStream為例

public void test1() throws Exception {

//定義字節(jié)輸出流對象

OutputStream osp = new FileOutputStream("D:/AAAkejian/Test/test1.txt");

String str = "Add abcd";

//將字符串轉(zhuǎn)化為字節(jié)存入字節(jié)數(shù)組中

byte[] bList = str.getBytes();

//寫入文件

osp.write(bList);

//刷新流

osp.flush();

//關(guān)閉流

osp.close();

}

2)字節(jié)輸入流——InputStream(所有字節(jié)輸入流的父類)

字節(jié)輸入流的使用,以FileInputStream為例,這里的讀取規(guī)則,與字符輸入流類似,利用循環(huán)進(jìn)行循環(huán)讀取文件內(nèi)容。

public void test() throws Exception{

//創(chuàng)建字節(jié)輸入流對象

InputStream ips = new FileInputStream("D:/AAAkejian/Test/test1.txt");

byte[] bList = new byte[3000];

int count = 0;

while( (count=ips.read(bList))!=-1 ){

//把byte數(shù)組轉(zhuǎn)換為字符串

String str=new String(bList,0,count);

System.out.println(str);

}

ips.close();

}

3)利用字節(jié)輸入輸出流完成圖片的復(fù)制功能

public void test()throws Exception{

//定義字節(jié)輸入流對象

InputStream ips = new FileInputStream("D:/AAAkejian/Test/test3.png");

//定義字節(jié)輸出流對象

OutputStream ops = new FileOutputStream("D:/AAAkejian/Test/test1.png");

//定義字節(jié)數(shù)組,用于存儲(chǔ)所讀取的內(nèi)容

byte[] bList = new byte[100];

int count =0;

while ((count = ips.read(bList))!=-1){

ops.write(bList,0,count);

ops.flush();

}

ops.close();

ips.close();

}

8.緩存流

緩存流是在基礎(chǔ)流(InputStream OutputStream Reader Writer)之上,添加了一個(gè)緩沖池的功能,用于提高IO效率,降低IO次數(shù)

緩沖流的使用:

public void test()throws Exception{

//定義字節(jié)輸出流

OutputStream ops = new FileOutputStream("D:/AAAkejian/Test/test1.txt");

//定義緩存流對象

BufferedOutputStream bfops = new BufferedOutputStream(ops);

String str = "new content";

byte[] bList = str.getBytes();

//此時(shí)的內(nèi)容在緩沖池中,并未寫入文件

bfops.write(bList);

//刷新緩沖池,將緩沖池中的內(nèi)容寫入文件中

//bfops.flush();

//h緩存流中的close方法,會(huì)先執(zhí)行flush方法

bfops.close();

}

9.對象流

對象流的意義在于數(shù)據(jù)的持久化,例如游戲存到,就是一種對象流的使用。

在日常程序運(yùn)行中,內(nèi)容都是存儲(chǔ)在內(nèi)存中的,而將內(nèi)存中的數(shù)據(jù),存儲(chǔ)到磁盤中,就實(shí)現(xiàn)了數(shù)據(jù)的持久化。

1)對象流輸出的使用——ObjectOutputStream--序列化(存檔):

public class Role implements Serializable {

private String name;

private int level;

private String power;

public Role(String name, int level, String power) {

this.name = name;

this.level = level;

this.power = power;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getLevel() {

return level;

}

public void setLevel(int level) {

this.level = level;

}

public String getPower() {

return power;

}

public void setPower(String power) {

this.power = power;

}

}

public void test()throws Exception{

OutputStream ops = new FileOutputStream("d:/AAAkejian/Test/test3.txt");

ObjectOutputStream oops = new ObjectOutputStream(ops);

//使用對象流調(diào)用輸出流的輸出方法,被輸出的對象的類必須實(shí)現(xiàn)Serializable接口

Role r1 = new Role("gjx",55,"撒潑打滾");

oops.writeObject(r1);

oops.close();

}

2)對象輸入流的使用——ObjectInputStream--反序列化(讀檔):

public void test()throws Exception{

InputStream ips = new FileInputStream("D:/AAAkejian/Test/test3.txt");

ObjectInputStream oips = new ObjectInputStream(ips);

Object o = oips.readObject();

System.out.println(o);

oips.close();

}

分享到:
標(biāo)簽:File
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定