參考書:《Java程序設(shè)計(jì)標(biāo)準(zhǔn)教程》
一、簡(jiǎn)介
java輸入輸出功能來自java.io.的抽象類:InputStream和OutputStream類、Reader和Writer類以及她們的子類仙畦,以流的形式處理數(shù)據(jù)坎背。流是一組數(shù)據(jù)序列,JAVA中每個(gè)數(shù)據(jù)流都是一個(gè)對(duì)象赔硫,提供各種支持讀入和寫出操作流類
二、I/0的基本模式
三盐肃、以byte為數(shù)據(jù)單位的流類(InputStream爪膊、OutputStream)
3.1、InputStream類
基本方法:
- Avilabel() 返回當(dāng)前輸入流的數(shù)據(jù)讀取方法可以讀取的有效字節(jié)數(shù)量砸王,返回int
- Read() 從當(dāng)前數(shù)據(jù)流中讀取一個(gè)字節(jié)推盛,如果已經(jīng)到達(dá)流結(jié)尾,則返回-1 返回 abstarct int
- Read(byte[] byte) 從當(dāng)前輸入流讀取一定的byte數(shù)據(jù)谦铃,并存放到數(shù)組中耘成,然后返回讀取byte數(shù)據(jù)的數(shù)量,如果已經(jīng)到到流結(jié)尾驹闰,則返回-1
- read(byte[] bytes, int off, int len)將當(dāng)前輸入流讀取一定的byte數(shù)據(jù)瘪菌,并存放在數(shù)組中指定位置。然后返回讀取byte數(shù)據(jù)的數(shù)量嘹朗,如果已經(jīng)到到流結(jié)尾师妙,則返回-1
- Reset() 將當(dāng)前輸入流重新定位到最后一次調(diào)用mark()方法時(shí)的位置
- Mark(int readlimit) 在當(dāng)前輸入流中做標(biāo)記位置,當(dāng)調(diào)用Reset()方法時(shí)將返回到該位置屹培,從標(biāo)記位置開始默穴,到再讀入readlimit這個(gè)字符為止,這個(gè)標(biāo)記都維持有效惫谤。
- Marksupported ()測(cè)試當(dāng)前輸入流是否支持mark壁顶、Reset方法,只要其中一個(gè)不支持溜歪,則返回false
- Skip(long n) 跳過和丟棄當(dāng)前輸入流中的n個(gè)字節(jié)數(shù)據(jù)
- Close()關(guān)閉當(dāng)前輸入流若专、并釋放任何與之相關(guān)聯(lián)的系統(tǒng)資源
InputStream is = System.inl // 獲取控制臺(tái)的輸入流
// InputStream is = null ;
Try {
// is = new InputStream();
Byte[] bs = new byte[1024];
Int i = is.read(bs); // 從輸入流中讀取數(shù)據(jù)到字節(jié)數(shù)組
Is.close(); // 關(guān)閉輸入流
} catch (IOException e){
E.printStackTrace()
}
3.2、OutputStream類
- Write(byte[] bw)將byte[]數(shù)組中的數(shù)據(jù)寫入當(dāng)前輸出流
- Write(byte[] b, int off, int len)將byte[]數(shù)組下標(biāo)off開始的長(zhǎng)度為len的數(shù)據(jù)寫入當(dāng)前的輸出流
- Write(int b)寫入一個(gè)byte數(shù)據(jù)到當(dāng)前的輸入流
- Flush() void 刷新當(dāng)前輸出流蝴猪,并強(qiáng)制寫入所有緩沖的字節(jié)數(shù)據(jù)
- Close() void 關(guān)閉當(dāng)前輸出流 并釋放所有與當(dāng)前輸出流有關(guān)的系統(tǒng)資源
實(shí)例:以控制臺(tái)輸出流
OutputStream out = null;
Try{
out = new OutputStream() ;
Byte[] bs = “實(shí)例是outputStream 輸出流”调衰。getBytes()膊爪;// 將字符串變成字節(jié)數(shù)組
Out.write(bs); // 將字節(jié)數(shù)組的內(nèi)容寫到輸出流
Out.close(); // 關(guān)閉輸出流
}catch(Exception e){
E.printStackTrace()
}
四、以char為數(shù)據(jù)單位的流類(Reader嚎莉、writer類)
4.1米酬、Reader類
- Ready()boolean 判斷此數(shù)據(jù)流是否準(zhǔn)備好
- Read()int 讀取一個(gè)字符,如果已讀到流結(jié)尾趋箩,返回-1
- Read(char[] chards, int off, int len) abstract int 讀取一些字符到char[]數(shù)組赃额,下標(biāo)off開始到len長(zhǎng)度,并返回所讀入的字符的數(shù)量叫确,如果到流結(jié)尾跳芳,返回-1
- Reset() void 在當(dāng)前輸入流重新定位到最后一次調(diào)用mark()方法時(shí)的位置
- Mark(int readlimit)
- Marksupported
- Skip(long n) long 跳過參數(shù)n指定的字符數(shù)量,并返回所跳過的字符數(shù)量
- Close()abstract void 關(guān)閉該流并釋放與之關(guān)聯(lián)的所有資源竹勉,在關(guān)閉流后飞盆,再調(diào)用read、ready次乓、mark吓歇、Reset、skip將拋出的異常票腰,關(guān)閉已經(jīng)關(guān)閉的流無效
實(shí)例:
InputStreamReader isr = null;
Try {
Isr = new InputStream(????????????????????????????);// 獲取輸入流
Char[] cs = new char[]; //??????????
Isr.read(cs)
String s = new String(cs);
Isr.close();
}catch(Exception e) {
}
4.2城看、Writer類
- Write(char[] cb) 返回void 將字符數(shù)組寫入字符輸出流
- Write(char[] cb, int off, int len)
- Write(int c);向一個(gè)輸出流寫一個(gè)字符數(shù)據(jù)
- Write(String str) 向一個(gè)輸出流寫入一個(gè)字符串?dāng)?shù)據(jù)
- Write(String str, int off, int len)
- Flush()刷新當(dāng)前輸出流,并強(qiáng)制寫入所有緩沖區(qū)的字節(jié)數(shù)據(jù)
- Close() 向輸出流寫入緩沖區(qū)的數(shù)據(jù)杏慰,然后關(guān)閉當(dāng)前輸出流析命,并釋放所有與當(dāng)前輸出流有關(guān)的系統(tǒng)資源
五、常用I/0處理方式
5.1逃默、文件類
- File類 用于封裝系統(tǒng)的文件和目錄的相關(guān)信息,文件大小簇搅、修改時(shí)間完域、文件路徑等
- File(String path) --- 指定文件文件路徑字符串創(chuàng)建一個(gè)新的File類的實(shí)例對(duì)象
- File(String parent, String child)
常用方法:
getName 獲取文件名稱
getParent 獲取文件父路徑字符串
getPath 獲取文件相對(duì)路徑字符串
getAbsolutePath 獲取文件的絕對(duì)路徑字符串
Exists 判斷是否存在
isFile 判斷是否為文件類型
isDirectory 判斷是否為文件夾類型
isAbsolute boolean 判斷是否是絕對(duì)路徑
Delete boolean 創(chuàng)建文件或文件夾,如果刪除成功返回true
Mkdir boolean 創(chuàng)建文件或文件夾瘩将,如果創(chuàng)建成功返回true
setReadOnly 設(shè)置文件夾的只讀屬性
Length long 獲取文件長(zhǎng)度
lastModified long 獲取文件的最后修改日期
List() String[] 獲取文件夾中的文件和子文件夾的名稱吟税,并存放到字符串?dāng)?shù)組中
listFiles() 返回File[] 。姿现。肠仪。。备典。异旧,并存放到File類型的數(shù)組中;
5.3提佣、文件的字節(jié)輸入輸出流
????程序運(yùn)行期間吮蛹,大部分?jǐn)?shù)據(jù)是在內(nèi)存中操作的荤崇,當(dāng)程序關(guān)閉或結(jié)束,這些數(shù)據(jù)完全消失潮针,但有些數(shù)據(jù)需要永久保存术荤;使用文件輸入輸出流可以和指定的文件建立關(guān)聯(lián),然后把需要永久保存的數(shù)據(jù)輸出到文件中每篷,程序下次運(yùn)行瓣戚,可以從文件中取回這些數(shù)據(jù);
????5.3.1創(chuàng)建文件字節(jié)輸入流
????文件字節(jié)輸入流可以從指定路徑的文件中讀取字節(jié)數(shù)據(jù)焦读,文件字節(jié)輸入流繼承字InputStream類子库,并實(shí)現(xiàn)了讀取輸入流的各種方法,
創(chuàng)建文件字節(jié)輸入流的常用構(gòu)造方法:
1吨灭、FileInputStream(File file)-----new FileInputStream(file),使用File類型的文件對(duì)象創(chuàng)建FileInputStream類的實(shí)例對(duì)象(文件字節(jié)輸入流)
2刚照、FileInputStream(String s) ---s指文件的絕對(duì)路徑、相對(duì)路徑
3喧兄、創(chuàng)建文件字節(jié)輸出流:關(guān)聯(lián)指定路徑的文件 无畔,數(shù)據(jù)通過文件字節(jié)輸出流以字節(jié)為單位輸出并保存到文件中。常用構(gòu)造方法FileOutputStream(File file),FileOutputStram(String fileName)
// 寫入文件
Private static String fileName = “xxxx”
Private static File file = new File(fileName);
// 將啟動(dòng)時(shí)間點(diǎn)存到文件 XXX中
Private static long date = System.currentTime();
// 文件開始寫入
String dataStr;
Try{
If(!file.exist()) {
File.createNewFile(); // ?????????????????注意區(qū)分 mkdir(創(chuàng)建文件吠冤,只能用來創(chuàng)建文件夾浑彰,且只能創(chuàng)建一級(jí)目錄,如果上級(jí)不存在拯辙,創(chuàng)建失敼洹)\makdirs(只能用來創(chuàng)建文件夾,涯保,能創(chuàng)建多級(jí)目錄诉濒,如果上級(jí)不存在,自動(dòng)創(chuàng)建夕春,創(chuàng)建文件夾多用這個(gè))\createNewFile(只能用來創(chuàng)建文件未荒,且只能創(chuàng)建在存在的目錄下創(chuàng)建文件,否則創(chuàng)建失敗及志,)-----
}
dataStr = date + “”;
Byte[] data = dataStr.getBytes();
FileOutputStream os = new FileOutputStream(file);
Os.write(data); // 將數(shù)據(jù)流寫入輸出流
Os.flush(); // 刷新緩沖區(qū)
Os.close(); // 關(guān)閉輸出流
}catch(Exception e) {
E.printStackTrace();
}
// 讀取文件
Try{
If(!file.exist()){
File.createNewFile();
}
Byte[] data = new Byte[1024];
FileInputStream fis = new FileInputStream(file); //創(chuàng)建文件輸入流
Int rs = 0;
String result;
While((rs = fis.read(data)) > 0) { //在循環(huán)中讀取輸入流的數(shù)據(jù)
Result += new String(data, 0, rs)
}
Fis.close();
}catch(Exception e){
E.printStackTrace();
}
4片排、文件的字符輸入輸出流
字節(jié)流一字節(jié)為單位傳送數(shù)據(jù),可以是任何類型的數(shù)據(jù)速侈,文本率寡,音頻,視頻倚搬,圖片等冶共,字符流以字符為單位傳送數(shù)據(jù),只能傳送文本類型數(shù)據(jù)
FileReader(File file)
FileReader(String fileName)
FileWriter(File file)
FileWriter(String fileName)
Public void readFile(){
Try{
String filePath = “xxxx”;
File file = new File(filePath);
FileReader reader = new FileReader(file);
Char[] data = new Char[512];
StringBuider sb = new StringBuilder();
Int rs = 0;
While((rs = reader.read(data)) > 0) {
sb .append(data, 0 , rs);
}
Reader.close();
}catch(Exception e) {
E.printStackTrace();
}
}
// 寫入文件
Public void writeFile() {
Try{
String filePath = “xxxx”;
File file = new File(filePath);
FileWriter fw = new FileWriter(file);
Fw.write(.......)//將數(shù)據(jù)寫入
}catch(Exception e) {
E.printStackTrace();
}
}
六、對(duì)象序列化
程序運(yùn)行時(shí)需要保存一些數(shù)據(jù)比默,對(duì)于基本數(shù)據(jù)類型幻捏,直接保存到文件中,但對(duì)于復(fù)雜對(duì)象類型命咐,需要將對(duì)象中不同的屬性分解為基本數(shù)據(jù)類型篡九,然后分別存到文件中,當(dāng)程序再次運(yùn)行時(shí)醋奠,需要建立新的對(duì)象榛臼,然后從文件中讀取與對(duì)象相關(guān)的所有數(shù)據(jù),再使用這些數(shù)據(jù)分別為對(duì)象的每個(gè)屬性初始化
ObjectInput\ObjectOutput接口繼承了DataInput和DataOutput接口窜司,提供對(duì)基本數(shù)據(jù)類型和對(duì)象序列化的方法沛善,使用對(duì)象序列化可以方便將對(duì)象寫入輸出流或者從輸入流讀取對(duì)象數(shù)據(jù)。
1塞祈、readObject()反序列化方法金刁,從輸入流中獲取序列化的對(duì)象數(shù)據(jù),用這些數(shù)據(jù)生成新的JAVA對(duì)象议薪,在ObjectInput接口中尤蛮,由ObjectInputStream類實(shí)現(xiàn)
Object object = readObject();注意類型的轉(zhuǎn)換,根據(jù)所需
2斯议、writeObject序列化方法
將對(duì)象寫入輸出流产捞,輸出流可以是文件輸出流、網(wǎng)絡(luò)輸出流哼御、其他輸出流
writeObject(object) 注意:被序列化的對(duì)象必須實(shí)現(xiàn)serializable接口坯临,否則無法實(shí)現(xiàn)序列化
Public class Worker implement Serializable{
Private String name;
Private String getName() {
Return name;}
Private void setName(String name) {
This.name = name;
}
}
Public static main(String[] args) {
Worker w = new Worker();
W.setName(“AAAA”);
// 序列化
Try {
FileOutputStream fo = new FileOutputStream(“./worker.dat”); // 在實(shí)例所在文件夾下創(chuàng)建文件worker.dat
ObjectOutputStream obj = new ObjectOutputStream(fo);
Obj.writeObject(worker)
}catch(Exception e) {
E.printStackTrace();
}
// 反序列化
Try{
FileInputStream fis = new FileInputStream(“./worker.dat”);
ObjectInputStream ois = new ObjectInputStream(fis);
Worker wor = (Worker)ois.readObject();
String name = wor.getName();
}catch(Exception e) {
E.printStackTrace();
}
}
七、對(duì)文件及文件夾操作
7.1 復(fù)制文件
指將文件內(nèi)容復(fù)制到指定位置的新文件中恋昼,如果目標(biāo)文件不存在看靠,以目標(biāo)文件名創(chuàng)建新的文件,并將原有文件內(nèi)容復(fù)制到該文件中液肌,否則覆蓋目標(biāo)文件
String sourceFilePath = “./A.txt”;
String dFilePath = “./B.txt”;
File sourceFile= new File(sourceFilePath); // 創(chuàng)建原文件對(duì)象
File dFile = new File(dFilePath); // 創(chuàng)建目標(biāo)文件對(duì)象
Try{
FileInputStream fis = new FileInputStream(sourceFile); // 創(chuàng)建原文件字節(jié)輸入流
FIleOutputStream fosD = new FileOutputStream(dFile); // 創(chuàng)建目標(biāo)文件字節(jié)輸出流
Byte[] data = new Byte[1024];
Int rs = -1;
While((rs = fis.read(data)) > 0) { // 在循環(huán)中讀取輸入流的數(shù)據(jù)
fosD.write(data, 0, rs); // 在循環(huán)中把數(shù)據(jù)寫入輸出流中
}
Fis.close();
fosD.close();
}catch(FileNotFoundException e) {
E.printStackTrace();
}
7.2 復(fù)制文件夾
文件夾中可能含有多個(gè)文件衷笋,或文件夾,必須對(duì)他們分別執(zhí)行復(fù)制操作
Public void copy(File[] s, File d) {
If(!d.exists) {
d.mkdir();
}
For(int i = 0; i < s.length; i++) {
If(s[i].isFile()) { // 如果是文件類型矩屁,直接進(jìn)行復(fù)制操作
Try{
FileInputStream fis = new FileInputStream(s[i]);
FileOutputStream fos = new FileOutputStream(new File(d.getPath() + File.separator + s[i].getName()));
Int num = fis.avaliable();
Byte[] data = new byte[num];
If((fis.read(data)) != -1){
Out.write(data); // 復(fù)制文件內(nèi)容
}
Fos.close();
Fis.close();
} catch(Exception e) {
E.printStackTrace();
}
}
If(s[i].isDirectory) { // 如果是文件夾類型
File des =(new File(d.getPath()+ File.separator + s[i].getName());
Des.mkdir();
Copy(s[i].listFiles(), des); // 遞歸調(diào)用方法本身
}
}
}
7.3 刪除文件夾
File類的delete方法用來刪除指定文件,必須使用目標(biāo)文件路徑創(chuàng)建一個(gè)File類的實(shí)例對(duì)象
File file = new File(“./aaaa.txt”);
deleteFile(file);
Public void deleteFile(File file) {
If(!file.exists()) {
// 文件不存在爵赵,直接返回
Return吝秕;
}
Boolean rs = file.delete();
}
7.4 分行寫入文件
FileWriter類 封裝到BufferWriter類的緩沖字符流中,實(shí)現(xiàn)緩沖字符輸出流空幻,并可以newLine()方法實(shí)現(xiàn)數(shù)據(jù)的分行寫入
String filePath = “./file.txt”;
File file = new File(filePath);
Try{
FileWriter fw = new FileWriter(file); // 創(chuàng)建文件字符輸出流
BufferedWriter bw = new BufferedWriter(fw); // 使用緩沖數(shù)據(jù)流封裝輸出流
Bw.write(“分行寫入文件”);
Bw.newLine(); // 寫入換行符
Bw.write(“mmmm”);
Bw.flush(); // 刷新緩沖區(qū)
} catch(IOException e) {
E.printStackTrace();
}