IO流

01 復(fù)習(xí)

文件夾刪除

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流常見流父類復(fù)習(xí)

頂級父類們


03輸出流的使用步驟

輸出流:頂層父類是OutputStream【抽象類】,借助其子類FileOutputStream學(xué)習(xí)

輸出流的使用步驟:

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

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

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

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

2 寫【調(diào)用寫數(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:關(guān)【強(qiáng)調(diào)關(guān)流的方法】

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)建流時需要使用一下兩個構(gòu)造方法:

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ù)要設(shè)置為true

寫出換行

\r\n

\r

\n

String ls=System.lineSeparator

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

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

使用步驟:

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

FileInputStream的構(gòu)造方法

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

public FileInputStream(String filePath):關(guān)聯(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)關(guān)【關(guān)閉資源】

public void close()

07 文件的復(fù)制

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

/*

把dir1中文件beauty.jpg復(fù)制到dir2中去

思路:先把dir1中的beauty.jpg讀取到內(nèi)存,然后寫入到dir2中的beauty.jpg

步驟:

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

2)讀,寫【邊讀邊寫】

3)關(guān)【關(guān)輸入和輸出流的資源】

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)關(guān)【關(guān)輸入和輸出流的資源】? 先開后關(guān)

fos.close();

fis.close();

?

longtimeCost=System.currentTimeMillis()-t1;

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

?? }

}


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

/*

? ? 把dir1中文件beauty.jpg復(fù)制到dir2中去

? ? 思路:先把dir1中的beauty.jpg讀取到內(nèi)存,然后寫入到dir2中的beauty.jpg

步驟:

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

2)讀,寫【邊讀邊寫】

3)關(guān)【關(guān)輸入和輸出流的資源】

*/

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)關(guān)【關(guān)輸入和輸出流的資源】? 先開后關(guān)

? ? ? ? fos.close();

? ? ? ? fis.close();

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

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

? ? }

}

08字符輸入流的使用

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

使用步驟:

1)創(chuàng)

? ? FileReader的構(gòu)造方法

? ? - 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)關(guān)

? ? 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:關(guān)

fr.close();

?

?? }

}

10字符的輸出流使用

/*

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

?

使用步驟:

1)創(chuàng)

FileWriter的構(gòu)造方法

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

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

?

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

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

public FileWriter(String filePath, boolean append): 關(guān)聯(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)關(guān)

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:關(guān)

fw.close();

?? }

}

11 Properties屬性類的使用

/*

Properties屬性類,最終會實現(xiàn)Map,本質(zhì)上就是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);

?

?

?? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容