IO流

01 復習

文件夾刪除

public class Demo01

{

public static void main(String[] args) {

deleteFile(new File("D:/abc"));

}

public static void deleteFile(File file){

if (file.isFile()){

file.delete();//刪除文件

? ? ? ? }

if (file.isDirectory()){

File[] files = file.listFiles();

for (File f : files) {

deleteFile(f);

}

file.delete();//刪除文件夾

? ? ? ? }

}

}

02 IO流常見流父類復習

頂級父類們


03輸出流的使用步驟

輸出流:頂層父類是OutputStream【抽象類】,借助其子類FileOutputStream學習

輸出流的使用步驟:

1.創(chuàng)【創(chuàng)建流對象】

FileOutputStream(File file):創(chuàng)建文件輸出流以寫入由指定的File對象表示的文件裹芝。

public FileoutputStream(String name):創(chuàng)建文件輸出流以指定的名稱寫入文件衙猪。

【如果原來文件存在數(shù)據(jù),會清空,如果沒有文件會創(chuàng)建一個】

2 寫【調用寫數(shù)據(jù)的方法】

write方法:

public void write(int b):寫出一個字節(jié)

public void write(byte[] bytes):寫出一個字節(jié)數(shù)組

public void write(byte[] bytes,int off,int len)

3:關【強調關流的方法】

public void close()

public class Demo2

{

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

FileOutputStream fos=new FileOutputStream(new File("file01.txt"));

fos.write(98);

byte[] bytes ="abc我愛java".getBytes();

fos.write(bytes);

fos.write(bytes,0,2);

fos.close();

}

}



04 寫出換行及文件數(shù)據(jù)追加功能

如果要完成文件數(shù)據(jù)追加續(xù)寫

創(chuàng)建流時需要使用一下兩個構造方法:

public FileOutputStream(File file,boolean append):創(chuàng)建文件輸出流以寫入由指定的File對象表示的文件。

public FileOutputStream(File file,boolean append):創(chuàng)建文件輸出流以寫入由指定的File對象表示的文件。

public FileOutputStream(String name,boolean append):創(chuàng)建文件輸出流以指定的名稱寫入文件榛了。

如果要完成文件數(shù)據(jù)的追加,append參數(shù)要設置為true

寫出換行

\r\n

\r

\n

String ls=System.lineSeparator

06字節(jié)輸入流的使用

輸入流頂層父類是:InputStream【抽象類】可以使用FileInputStream

使用步驟:

1)創(chuàng)【創(chuàng)建輸入流對象】

FileInputStream的構造方法

public FileInputStream(File file):關聯(lián)要讀取的文件

public FileInputStream(String filePath):關聯(lián)要讀取的文件路徑

要求指定的文件煞抬,一定要存在霜大,否則會報錯

【FileNotFoundException】

2)讀【讀取數(shù)據(jù)】

read:

public int read():讀取一個字節(jié)并返回,如果讀取到末位革答,會返回-1

public int read(byte[] bytes):讀取多個數(shù)據(jù)到字節(jié)數(shù)組战坤,返回讀取有效的字節(jié)個數(shù),如果沒有讀到數(shù)據(jù)返回-1

3)關【關閉資源】

public void close()

07 文件的復制

低效復制:int read()void write()

/*

把dir1中文件beauty.jpg復制到dir2中去

思路:先把dir1中的beauty.jpg讀取到內存残拐,然后寫入到dir2中的beauty.jpg

步驟:

1)創(chuàng):【創(chuàng)建輸入輸出流】

2)讀途茫,寫【邊讀邊寫】

3)關【關輸入和輸出流的資源】

public class Demo01{

publicstaticvoidmain(String[]args)throwsIOException{

longt1=System.currentTimeMillis();

//1)創(chuàng):【創(chuàng)建輸入輸出流】

FileInputStreamfis=newFileInputStream("dir1/beauty.jpg");

FileOutputStreamfos=newFileOutputStream("dir2/beauty.jpg");

//2)讀,寫【邊讀邊寫】

intb;

while((b=fis.read())!=-1) {

fos.write(b);

? ? ?? }

?

//3)關【關輸入和輸出流的資源】? 先開后關

fos.close();

fis.close();

?

longtimeCost=System.currentTimeMillis()-t1;

System.out.println("timeCost = "+timeCost);

?? }

}


高效的復制(int read(byte[] bytes) void write(byte[] bytes,int off,int len))

/*

? ? 把dir1中文件beauty.jpg復制到dir2中去

? ? 思路:先把dir1中的beauty.jpg讀取到內存溪食,然后寫入到dir2中的beauty.jpg

步驟:

1)創(chuàng):【創(chuàng)建輸入輸出流】

2)讀囊卜,寫【邊讀邊寫】

3)關【關輸入和輸出流的資源】

*/

public class Demo02 {

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

? ? ? ? long t1 = System.currentTimeMillis();

? ? ? ? //1)創(chuàng):【創(chuàng)建輸入輸出流】

? ? ? ? FileInputStream fis = new FileInputStream("dir1/beauty.jpg");

? ? ? ? FileOutputStream fos = new FileOutputStream("dir2/beauty5.jpg");

? ? ? ? //2)讀,寫【邊讀邊寫】

? ? ? ? int len;

? ? ? ? byte[] bytes = new byte[1024*8];

? ? ? ? while ((len = fis.read(bytes)) != -1) {

? ? ? ? ? ? fos.write(bytes,0,len); //寫出有效數(shù)據(jù)

? ? ? ? }

? ? ? ? //3)關【關輸入和輸出流的資源】? 先開后關

? ? ? ? fos.close();

? ? ? ? fis.close();

? ? ? ? long timeCost = System.currentTimeMillis() - t1;

? ? ? ? System.out.println("timeCost = " + timeCost);

? ? }

}

08字符輸入流的使用

字符的輸入流:Reader【抽象類】错沃,得其子類 FileReader

使用步驟:

1)創(chuàng)

? ? FileReader的構造方法

? ? - FileReader(File file): 創(chuàng)建一個新的 FileReader 栅组,給定要讀取的File對象。

? ? - FileReader(String fileName): 創(chuàng)建一個新的 FileReader 枢析,給定要讀取的文件的名稱玉掸。

? ? 如果文件不存在,會報錯FileNotFoundException

2)讀

? ? read:

? ? public int read():讀取一個字符醒叁,如果讀取到末位司浪,返回-1

? ? public int read(char[] chars) :讀取有效的字符到字符數(shù)組,返回讀取字符的有效個數(shù)辐益,如果沒有字符返回-1

3)關

? ? public void close()

*/

public class Demo01{

public static void main(String[]args)throwsIOException{

//1:創(chuàng)

FileReaderfr=newFileReader("file05.txt");

//2:讀

?

/* // public int read():讀取一個字符断傲,如果讀取到末位,返回-1

int c;//保存讀取的字符

while ((c = fr.read()) != -1) {

System.out.println((char)c);

}*/

?

// public int read(char[] chars) :讀取有效的字符到字符數(shù)組智政,返回讀取字符的有效個數(shù)认罩,如果沒有字符返回-1

intlen;

char[]chars=newchar[2];

while((len=fr.read(chars))!=-1) {

System.out.println(newString(chars,0,len));

? ? ?? }


//3:關

fr.close();

?

?? }

}

10字符的輸出流使用

/*

字符輸出流:Writer【抽象類】,得用其子類FileWriter

?

使用步驟:

1)創(chuàng)

FileWriter的構造方法

public FileWriter(File file): 關聯(lián)一個文件用來保存寫出的數(shù)據(jù)

public FileWriter(String filePath): 關聯(lián)一個文件用來保存寫出的數(shù)據(jù)

?

//如果append設置為true续捂,具有數(shù)據(jù)追加拼接的功能

public FileWriter(File file ,boolean append): 關聯(lián)一個文件用來保存寫出的數(shù)據(jù)

public FileWriter(String filePath, boolean append): 關聯(lián)一個文件用來保存寫出的數(shù)據(jù)

?

2)寫

write:

public void write(int c):寫出一個字符

public void write(char[] chars):寫出一個字符數(shù)組

public void write(char[] chars,int off,int len):寫出一個字符數(shù)組中指定字符

public void write(String str):直接寫出字符串

?

3)關

public void close()

?

*/

publicclassDemo01{

publicstaticvoidmain(String[]args)throwsIOException{

//1:創(chuàng)

FileWriterfw=newFileWriter("file06.txt");

//2:寫

fw.write('A');//寫出一個字符

char[]chars={'a','b','c'};

fw.write(chars);//寫出一個字符數(shù)組

fw.write(chars,0,2);//寫出字符數(shù)組中指定的字符

fw.write("HelloWorld");//寫出字符串

fw.write("我愛Java",0,2);//寫出字符串中指定的字符

//3:關

fw.close();

?? }

}

11 Properties屬性類的使用

/*

Properties屬性類垦垂,最終會實現(xiàn)Map,本質上就是Map特點就是保存鍵值對信息

用來存儲:屬性配置信息【鍵值對】

downloadPath=D:/download

?

1)創(chuàng)建對象

2)使用方法

- public Object setProperty(String key, String value) : 保存一對屬性牙瓢。 ? put

- public String getProperty(String key) :使用此屬性列表中指定的鍵搜索屬性值劫拗。 get

- public Set<String> stringPropertyNames() :所有鍵的名稱的集合。 keySet

?

從流中讀取

- public void load(InputStream in): 從字節(jié)輸入流中讀取屬性文件矾克。

- public void load(Reader reader): 從字符流中讀取屬性文件

存入到流中

- public void store(OutputStream out,String comment): comment是注釋

- public void store(Writer writer, String comment):

?

?

*/

publicclassDemo01{

publicstaticvoidmain(String[]args)throwsIOException{

?

Propertiespro=newProperties();

pro.setProperty("downloadPath1","D:/download");

pro.setProperty("downloadPath2","D:/music");

pro.setProperty("downloadPath3","D:/movie");

System.out.println("pro = "+pro);

?

StringaaaValue=pro.getProperty("aaa","沒有配置信息");

System.out.println("aaaValue = "+aaaValue);

StringdownloadPath=pro.getProperty("downloadPath");

System.out.println("downloadPath = "+downloadPath);

?

Set<String>keySet=pro.stringPropertyNames();

System.out.println("keySet = "+keySet);

?

// 存入到流中:存儲到指定的路徑

//- public void store(OutputStream out,String comment): comment是注釋

//- public void store(Writer writer, String comment):

pro.store(newFileWriter("downloadInfo.properties"),"this is comment");


?? }

}

/*

從流中讀取

- public void load(InputStream in): 從字節(jié)輸入流中讀取屬性文件页慷。

- public void load(Reader reader): 從字符流中讀取屬性文件

?

*/

publicclassDemo02{

publicstaticvoidmain(String[]args)throwsIOException{

Propertiespro=newProperties();

System.out.println("pro = "+pro);

?

pro.load(newFileReader("downloadInfo.properties"));

?

System.out.println("pro = "+pro);

?

?

?? }

}

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子酒繁,更是在濱河造成了極大的恐慌滓彰,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件州袒,死亡現(xiàn)場離奇詭異揭绑,居然都是意外死亡,警方通過查閱死者的電腦和手機郎哭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進店門他匪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人夸研,你說我怎么就攤上這事邦蜜。” “怎么了陈惰?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵畦徘,是天一觀的道長。 經(jīng)常有香客問我抬闯,道長井辆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任溶握,我火速辦了婚禮杯缺,結果婚禮上,老公的妹妹穿的比我還像新娘睡榆。我一直安慰自己萍肆,他們只是感情好,可當我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布胀屿。 她就那樣靜靜地躺著塘揣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪宿崭。 梳的紋絲不亂的頭發(fā)上亲铡,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天,我揣著相機與錄音葡兑,去河邊找鬼奖蔓。 笑死,一個胖子當著我的面吹牛讹堤,可吹牛的內容都是我干的吆鹤。 我是一名探鬼主播,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼洲守,長吁一口氣:“原來是場噩夢啊……” “哼疑务!你這毒婦竟也來了沾凄?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤知允,失蹤者是張志新(化名)和其女友劉穎搭独,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體廊镜,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年唉俗,在試婚紗的時候發(fā)現(xiàn)自己被綠了嗤朴。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡虫溜,死狀恐怖雹姊,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情衡楞,我是刑警寧澤吱雏,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站瘾境,受9級特大地震影響歧杏,放射性物質發(fā)生泄漏。R本人自食惡果不足惜迷守,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一犬绒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧兑凿,春花似錦凯力、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至圣絮,卻和暖如春祈惶,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背晨雳。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工行瑞, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人餐禁。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓血久,卻偏偏與公主長得像,于是被迫代替她去往敵國和親帮非。 傳聞我的和親對象是個殘疾皇子氧吐,可洞房花燭夜當晚...
    茶點故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內容