一、IO流概述
如上圖腕侄,我們經(jīng)常會(huì)從服務(wù)器下載一些軟件保存在本地,在這個(gè)傳輸中就涉及到IO流慈省。
我們來看這個(gè)過程区岗,我們將服務(wù)器硬盤里的文件放在內(nèi)存中略板,在這個(gè)過程我們稱之為輸入流(InPutStream)或讀(Read)。然后我們將文件通過網(wǎng)線傳輸?shù)轿覀兊腜C中慈缔,在這個(gè)過程中叮称,相對(duì)于服務(wù)器而言,我們稱之為輸出流(OutPutStream)或?qū)懀╓rite)藐鹤,而相對(duì)于PC瓤檐,我們稱之為輸入流(InPutStream)或讀(Read)。而將PC內(nèi)存中的文件保存在硬盤中時(shí)娱节,我們稱之為輸出流(OutPutStream)或?qū)懀╓rite)挠蛉。
因此,我們將IO流按照其方向分為輸入流肄满、輸出流谴古。他們的本質(zhì)實(shí)現(xiàn)對(duì)內(nèi)存而言绍移,輸入流表示將文件保存至內(nèi)存,輸出流表示將文件從內(nèi)存中取出讥电。按照其讀取方式蹂窖,分為字符流和字節(jié)流,字符流一次讀取2個(gè)字節(jié)恩敌。
二瞬测、字符流
1 FileInputStream 字節(jié)輸入流
1.1 創(chuàng)建FileInputStream:
創(chuàng)建FileInputStream
有2種構(gòu)造方法,即:FileInputStream(File file)
和FileInputStream(String name)
使用FileInputStream(String name)
時(shí)我們需要傳入文件的路徑纠炮,第一種方式傳入相對(duì)路徑月趟,第二三種傳入絕對(duì)路徑
//String filepath="temp.txt";
//String filepath="E:\\Screen\\temp.txt";
String filepath="E:/Screen/temp.txt";
FileInputStream fis=new FileInputStream(filepath);
1.2 讀取
1.2.1 read()方法,每次讀取一個(gè)字節(jié)
read()
方法每次讀取一個(gè)字節(jié)恢口,當(dāng)無數(shù)據(jù)可讀取時(shí)孝宗,會(huì)返回-1,注意漢字占2個(gè)字節(jié)
缺點(diǎn):頻繁訪問磁盤且效率低
int temp=0;
while((tem==fis.read())耕肩!=-1){
System.out..println(temp);
}
1.2.2 read(byte[] b)方法因妇,將數(shù)據(jù)讀取至byte數(shù)組中,方法表示每次讀取多個(gè)字節(jié)
方法返回int
類型的值猿诸,表示這次讀取了多少個(gè)字節(jié)婚被,已經(jīng)讀到末尾,返回-1
byte[] bytes=new byte[1024];//每次讀取1kb
int temp=0;
while((temp=fis.read(bytes))!-1){
System.out..println(bytes,0,temp);
}
1.3 關(guān)閉流
最后我們?cè)?code>finally語句塊中關(guān)閉流
finally{
try{
if(fis!=null){
fis.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
2 FileOutputStream 字節(jié)輸出流
2.1 創(chuàng)建FileOutputStream
FileOutputStream
的構(gòu)造方法和FileInputStream
基本相同
FileOutPutStream(String filepath,bool append)
,方法中apeend
表示是否可以向文件中追加內(nèi)容
FileOutputStream fos=new FileOutputStream(filepath);
2.3 寫入數(shù)據(jù)
//將msg寫入硬盤
String msg="Hello Java";
//將字符串轉(zhuǎn)換為byte數(shù)組
byte[] bytes=msg.getBytes();
fos.write(bytes);
//推薦最后為了保證數(shù)據(jù)完全寫入硬盤梳虽,因此需要執(zhí)行刷新操作
fos.flush();//強(qiáng)制寫入
2.3 關(guān)閉流
三址芯、字節(jié)流
字節(jié)流
1 FileReader 字節(jié)輸入流
1.1 創(chuàng)建FileReader
FileReader
的構(gòu)造方法與FileInPutStream
相同
FileReader fr=new FileReader(filepath);
1.2 讀取
chart[] charts=new Char[512];//讀取1kb
int temp=0;
while((temp=fr.read(charts))!=-1){
System.out.println(new String(charts,0,temp));
}
1.3 關(guān)閉流
2 FileWriter 字節(jié)輸出流
FileWriter
的構(gòu)造方法和FileOutputStrem
的構(gòu)造方法相同
2.1 創(chuàng)建FileWriter
FileWritet fw=new FileWriter(filepath);
2.1 寫入數(shù)據(jù)
當(dāng)我們寫入數(shù)據(jù),我們可以調(diào)用以下方法
write(char[] cbuf);//寫入字符數(shù)組
write(int c);//寫入單個(gè)字符
write(String str);//寫入字符串
2.3 關(guān)閉流
四窜觉、緩沖流
BufferedReader br=new BufferedReader(new FileReader(filename));
Strint temo=null;
//調(diào)用readLine()讀取一行數(shù)據(jù)流
while((temp=br.readLine())!=null){
System.out.println(temp)
}
//關(guān)閉流
br.close();//只用關(guān)閉最外面的包裝流
BufferedWriter bw=new BufferedWriter(new FileWriter(filename));
bw.write(String str);
bw.newLine();//寫入行分隔符
bw.flush();
//關(guān)閉流
bw.close();
五谷炸、轉(zhuǎn)換流
FileInputStream fis=new FileInputStream(filepath);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
Strint temo=null;
//調(diào)用readLine()讀取一行數(shù)據(jù)流
while((temp=br.readLine())!=null){
System.out.println(temp)
}
//關(guān)閉流
br.close();//只用關(guān)閉最外層的包裝流
五、保存對(duì)象
序列化
//創(chuàng)建對(duì)象,這里的對(duì)象需要序列化(序列化:在Java中使我們的類實(shí)現(xiàn)Serilizable)
User user=new User();
ObjectOutputStream oos=new ObjectOutputStream(new FileioutputStrema(filepatj));
ooa.writeObject(oos);
oos.flush();
oos.close();
反序列化
ObjectInputStream ois=new ObjectStreann(nwq FileInputSteam(filepath));
ois.readObject();
ois.close();
六 File
- file和流無關(guān)禀挫,不能通過完成讀寫操作
- file是文件和目錄路徑名的抽象變現(xiàn)形式
File構(gòu)造方法:
File(Sting pathname);
File(URI uri );
其他方法:
File.exists();
//判斷文件或目錄是否存在
File.mkdir()
//創(chuàng)建目錄
File.createnewFile()
//創(chuàng)建文件
File.canExecute()
// 測試應(yīng)用程序是否可以執(zhí)行此抽象路徑名表示的文件旬陡。
File.canRead()
// 測試應(yīng)用程序是否可以讀取此抽象路徑名表示的文件。
File.canWrite()
·試應(yīng)用程序是否可以修改此抽象路徑名表示的文件特咆。
File.delete()
// 刪除此抽象路徑名表示的文件或目錄季惩。
File.getAbsoluteFile()
// 返回此抽象路徑名的絕對(duì)路徑名形式。
File.getAbsolutePath()
// 返回此抽象路徑名的絕對(duì)路徑名字符串腻格。
File.getName()
// 返回由此抽象路徑名表示的文件或目錄的名稱画拾。
File. getParent()
返回此抽象路徑名父目錄的路徑名字符串;如果此路徑名沒有指定父目錄菜职,則返回 null青抛。
File. isAbsolute()
測試此抽象路徑名是否為絕對(duì)路徑名。
File.isDirectory()
測試此抽象路徑名表示的文件是否是一個(gè)目錄酬核。
File.isFile()
測試此抽象路徑名表示的文件是否是一個(gè)標(biāo)準(zhǔn)文件蜜另。
File.listFiles()
返回一個(gè)抽象路徑名數(shù)組适室,這些路徑名表示此抽象路徑名表示的目錄中的文件。