亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

一、Java IO 編程

系統(tǒng) 1856 0

Java IO 編程?
1、基本概念?
?? Java中對(duì)文件的操作是以流的方式進(jìn)行的,流是Java內(nèi)存中一組有序數(shù)據(jù)序列。Java將數(shù)據(jù)從源(文件、內(nèi)存、鍵盤、網(wǎng)絡(luò))讀入到內(nèi)存中,形成了流,然后還可以將這些流寫到另外的目的地(文件、內(nèi)存、控制臺(tái)、網(wǎng)絡(luò))之所以叫做流,是因?yàn)檫@個(gè)數(shù)據(jù)序列在不同時(shí)刻所操作的是源的不同部分。?
2、流的分類?
流的分類方式一般有以下三種:?
(1) 輸入的方向分:輸入流和輸出流,輸入和輸出的參照對(duì)象是Java程序。?
(2) 處理數(shù)據(jù)的單位分:字節(jié)流和字符流,字節(jié)流讀取的最小單位是一個(gè)字節(jié)。?
(3) 功能的不同分:節(jié)點(diǎn)流和處理流,一個(gè)是直接一個(gè)是包裝的。?
3、流分類的關(guān)系?
流分類的根源來(lái)自四個(gè)基本的類,這四個(gè)類的關(guān)系如下:?
?? 字節(jié)流 ?????????? 字符流?
輸入流 InputStream Reader?
輸出流 OutputStream Writer?
4、其他知識(shí)補(bǔ)充?
(1)什么是IO?
?? IO(Input/Output)是計(jì)算機(jī)輸入/輸出的接口,Java的核心庫(kù)java.io提供了全面的IO接口,包括:文件讀寫、標(biāo)準(zhǔn)設(shè)備輸出等等。Java中的IO是以流為基礎(chǔ)進(jìn)行輸入輸出的,所有數(shù)據(jù)被串行化寫入輸出流,或者從輸入流讀入。?
(2)流IO和塊IO?
?? 此外,Java也對(duì)塊傳輸提供支持,在核心庫(kù)java.nio中采用的便是塊IO,流IO和塊IO對(duì)比而言,流IO的好處是簡(jiǎn)單易用,缺點(diǎn)是效率不如塊IO;相反塊IO是效率比較高但是編程比較復(fù)雜。Java的IO模型設(shè)計(jì)非常優(yōu)秀,它使用了Decorator模式,按照功能進(jìn)行劃分stream,編程過(guò)程中可以動(dòng)態(tài)地裝配這些stream,以便獲取所需要的功能。?
?? 備注:以上資料提取自百度文庫(kù),鏈接地址如下:?
??? http://wenku.baidu.com/view/9aa0ec35eefdc8d376ee3280.html ?
5、代碼模擬實(shí)戰(zhàn)?

Java代碼?? 收藏代碼
  1. import ?java.io.File;??
  2. import ?java.io.IOException;??
  3. ??
  4. public ? class ?FileDemoTest?{??
  5. ???? /** ?
  6. ?????*?@description?基礎(chǔ)File類操作 ?
  7. ?????*?@param?args ?
  8. ?????*?@throws?IOException?IO異常處理 ?
  9. ?????*/ ??
  10. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  11. ???????? //?Windows系統(tǒng)下的文件目錄格式,這個(gè)和Unix系統(tǒng)等不同 ??
  12. ???????? //?String?pathSeparator?=?File.pathSeparator;?//?; ??
  13. ???????? //?String?separator?=?File.separator;?//?\ ??
  14. ??
  15. ???????? //?1、創(chuàng)建和刪除文件 ??
  16. ????????File?file?=? new ?File( "d:\\helloworld.txt" );??
  17. ???????? //?File?file?=?new?File("d:"+File.separator+"helloworld.txt"); ??
  18. ????????file.createNewFile();? //?創(chuàng)建文件 ??
  19. ???????? if ?(file.exists())?{? //?如果文件存在,則刪除文件 ??
  20. ????????????file.delete();??
  21. ????????}??
  22. ??
  23. ???????? //?2、創(chuàng)建文件夾操作 ??
  24. ????????File?file2?=? new ?File( "d:\\helloworld" );??
  25. ???????? //?File?file2?=?new?File("d:"+File.separator+"helloworld"); ??
  26. ????????file2.mkdir();? //?建立文件夾 ??
  27. ???????? if ?(file2.isDirectory())?{??
  28. ????????????System.out.println( "hello-directory" );? //?判斷是否是目錄 ??
  29. ????????}??
  30. ??
  31. ???????? //?3、遍歷文件或者文件夾操作 ??
  32. ????????File?file3?=? new ?File( "d:\\" );??
  33. ???????? //?File?file3?=?new?File("d:"+File.separator); ??
  34. ????????File?files[]?=?file3.listFiles();? //?列出全部?jī)?nèi)容 ??
  35. ???????? for ?( int ?i?=? 0 ;?i?<?files.length;?i++)?{??
  36. ????????????System.out.println(files[i]);??
  37. ????????}??
  38. ????}??
  39. }??

Java代碼?? 收藏代碼
  1. import ?java.io.File;??
  2. import ?java.io.IOException;??
  3. import ?java.io.InputStream;??
  4. import ?java.io.FileInputStream;??
  5. ??
  6. public ? class ?InputStreamDemo?{??
  7. ???? /** ?
  8. ?????*?@description?字節(jié)流:輸入流InputStream ?
  9. ?????*?@param?args ?
  10. ?????*?@throws?IOException ?
  11. ?????*/ ??
  12. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  13. ???????? //?字節(jié)流的輸入流和輸出流過(guò)程步驟比較固定 ??
  14. ???????? //?第1步、使用File類找到一個(gè)文件 ??
  15. ????????File?f?=? new ?File( "d:" ?+?File.separator?+? "helloworld.txt" );??
  16. ???????? //?第2步、通過(guò)子類實(shí)例化父類對(duì)象(InputStream為抽象類,本身不能直接實(shí)例化) ??
  17. ????????InputStream?input?=? new ?FileInputStream(f);??
  18. ???????? //?第3步、進(jìn)行讀操作 ??
  19. ???????? byte ?b[]?=? new ? byte [( int )f.length()];? //?數(shù)組大小由文件大小來(lái)確定 ??
  20. ???????? for ?( int ?i?=? 0 ;?i?<?b.length;?i++)?{??
  21. ????????????b[i]?=?( byte )?input.read();? //?讀取內(nèi)容 ??
  22. ????????}??
  23. ???????? //?第4步、關(guān)閉輸出流 ??
  24. ????????input.close();???
  25. ????}??
  26. }??

Java代碼?? 收藏代碼
  1. import ?java.io.File;??
  2. import ?java.io.IOException;??
  3. import ?java.io.OutputStream;??
  4. import ?java.io.FileOutputStream;??
  5. ??
  6. public ? class ?OutputStreamDemo?{??
  7. ???? /** ?
  8. ?????*@description?字節(jié)流:輸出流OutputStream類 ?
  9. ?????*@param?args ?
  10. ?????*@throws?IOException? ?
  11. ?????*/ ??
  12. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  13. ???????? //?輸入和輸出流參考的是Java程序,輸出流操作步驟也比較固定 ??
  14. ???????? //?第1步、使用File類找到一個(gè)文件 ??
  15. ????????File?f?=? new ?File( "d:" ?+?File.separator?+? "helloworld.txt" );??
  16. ???????? //?第2步、通過(guò)子類實(shí)例化父類對(duì)象 ??
  17. ???????? //OutputStream?out?=?new?FileOutputStream(f); ??
  18. ????????OutputStream?out?=? new ?FileOutputStream(f, true );??
  19. ???????? //?第3步、進(jìn)行寫操作 ??
  20. ????????String?str?=? "say?hello?world!!!" ;??
  21. ???????? byte ?b[]?=?str.getBytes();??
  22. ???????? for ( int ?i= 0 ;i<b.length;i++){???
  23. ????????????out.write(b[i]);? //?單個(gè)寫入 ??
  24. ????????}??
  25. ???????? //?out.write(b); ??
  26. ???????? //?重載方法write ??
  27. ???????? //?public?abstract?void?write(int?b)?throws?IOException; ??
  28. ???????? //?public?void?write(byte?b[])?throws?IOException?{} ??
  29. ???????? //?第4步、關(guān)閉輸出流 ??
  30. ????????out.close();? //?關(guān)閉輸出流 ??
  31. ????}??
  32. }??

Java代碼?? 收藏代碼
  1. import ?java.io.File;??
  2. import ?java.io.IOException;??
  3. import ?java.io.Reader;??
  4. import ?java.io.FileReader;??
  5. ??
  6. public ? class ?ReaderDemo?{??
  7. ???? /** ?
  8. ?????*@description?字節(jié)流和字符流按照處理數(shù)據(jù)的單位劃分 ?
  9. ?????*@param?args ?
  10. ?????*@throws?IOException?IO異常處理 ?
  11. ?????*/ ??
  12. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  13. ???????? //?第1步、使用File類找到一個(gè)文件 ??
  14. ????????File?f?=? new ?File( "d:" ?+?File.separator?+? "helloworld.txt" );??
  15. ???????? //?第2步、通過(guò)子類實(shí)例化父類對(duì)象 ??
  16. ????????Reader?input?=? new ?FileReader(f);??
  17. ???????? //?第3步、進(jìn)行讀操作 ??
  18. ???????? char ?c[]?=? new ? char [ 1024 ];??
  19. ???????? int ?temp?=? 0 ;??
  20. ???????? int ?len?=? 0 ;??
  21. ???????? while ?((temp?=?input.read())?!=?- 1 )?{??
  22. ???????????? //?如果不是-1就表示還有內(nèi)容,可以繼續(xù)讀取 ??
  23. ????????????c[len]?=?( char )?temp;??
  24. ????????????len++;??
  25. ????????}??
  26. ???????? //?第4步、關(guān)閉輸出流 ??
  27. ????????input.close();??
  28. ????}??
  29. }??

Java代碼?? 收藏代碼
  1. import ?java.io.File;??
  2. import ?java.io.IOException;??
  3. import ?java.io.Writer;??
  4. import ?java.io.FileWriter;??
  5. ??
  6. public ? class ?WriterDemo?{??
  7. ???? /** ?
  8. ?????*@description?通過(guò)代碼學(xué)習(xí)發(fā)現(xiàn),基本上步驟一致的 ?
  9. ?????*@param?args ?
  10. ?????*@throws?IOException ?
  11. ?????*/ ??
  12. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  13. ???????? //?第1步、使用File類找到一個(gè)文件 ??
  14. ????????File?f?=? new ?File( "d:" ?+?File.separator?+? "helloworld.txt" );??
  15. ???????? //?第2步、通過(guò)子類實(shí)例化父類對(duì)象 ??
  16. ????????Writer?out?=? new ?FileWriter(f);? //?通過(guò)對(duì)象多態(tài)性,進(jìn)行實(shí)例化 ??
  17. ???????? //?第3步、進(jìn)行寫操作 ??
  18. ????????String?str?=? "\nsay?hello\tworld" ;???
  19. ????????out.write(str);???
  20. ???????? //?第4步、關(guān)閉輸出流 ??
  21. ???????? //?out.flush();?//?清空緩沖 ??
  22. ????????out.close();? //?關(guān)閉輸出流 ??
  23. ??????????
  24. ???????? //?另外有緩沖功能的類為:BufferedReader ??
  25. ????}??
  26. }??

Java代碼?? 收藏代碼
  1. import ?java.io.ByteArrayInputStream;??
  2. import ?java.io.File;??
  3. import ?java.io.FileInputStream;??
  4. import ?java.io.FileOutputStream;??
  5. import ?java.io.IOException;??
  6. import ?java.io.InputStream;??
  7. import ?java.io.InputStreamReader;??
  8. import ?java.io.OutputStreamWriter;??
  9. import ?java.io.Reader;??
  10. import ?java.io.Writer;??
  11. ??
  12. public ? class ?ByteAndCharStreamTest?{??
  13. ???? /** ?
  14. ?????*@description??字節(jié)流:InputStream,OutputStream ?
  15. ?????*??????????????字符流:Reader,Writer?? ?
  16. ?????*??????????????//?字節(jié)流和字符流相互轉(zhuǎn)換測(cè)試 ?
  17. ?????*@param?args ?
  18. ?????*?@throws?IOException?文件操作異常處理 ?
  19. ?????*/ ??
  20. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  21. ???????? //?1、輸入流-字節(jié)流轉(zhuǎn)換成字符流 ??
  22. ???????? //?通過(guò)InputStreamReader類來(lái)進(jìn)行轉(zhuǎn)換 ??
  23. ????????InputStream?is?=? new ?FileInputStream( "d:\\helloworld.txt" );??
  24. ????????Reader?reader?=? new ?InputStreamReader(is);? //?將字節(jié)流轉(zhuǎn)換成字符流 ??
  25. ???????? char ?c[]?=? new ? char [ 1024 ];??
  26. ???????? int ?len?=?reader.read(c);? //?讀取操作,保存在字符數(shù)組中 ??
  27. ????????reader.close();? //?關(guān)閉操作 ??
  28. ????????System.out.println( new ?String(c, 0 ,len));? //?字符數(shù)組轉(zhuǎn)換成String類實(shí)例 ??
  29. ??????????
  30. ???????? //?2、輸出流-字節(jié)流轉(zhuǎn)換成字符流 ??
  31. ???????? //?通過(guò)OutputStreamWriter類來(lái)進(jìn)行轉(zhuǎn)換 ??
  32. ????????File?f?=? new ?File( "d:" ?+?File.separator?+? "helloworld.txt" );??
  33. ????????Writer?out?=? new ?OutputStreamWriter( new ?FileOutputStream(f));? //?字節(jié)流變?yōu)樽址? ??
  34. ????????out.write( "hello?world!!!" );? //?使用字符流輸出 ??
  35. ????????out.close();??
  36. ??????????
  37. ???????? //?3、從字符流到字節(jié)流轉(zhuǎn)換可以采用String類提供的操作 ??
  38. ???????? //?從字符流中獲取char[],轉(zhuǎn)換成String實(shí)例然后再調(diào)用String?API的getBytes()方法? ??
  39. ???????? //?接1中的代碼如下,最后通過(guò)ByteArrayInputStream即可完成操作 ??
  40. ????????String?str?=? new ?String(c, 0 ,len);??
  41. ???????? byte ?b[]?=?str.getBytes();??
  42. ????????InputStream?is2?=? new ?ByteArrayInputStream(b);??
  43. ????????is2.close();??
  44. ??????????
  45. ???????? //?4、其他常見(jiàn)的流 ??
  46. ???????? //?內(nèi)存操作流ByteArrayInputStream,ByteArrayOutputStream ??
  47. ???????? //?管道流PipedOutputStream,PipedInputStream ??
  48. ???????? //?打印流PrintStream ??
  49. ???????? //?緩存流BufferedReader ??
  50. ???????? //?...等等 ??
  51. ????}??
  52. }??

Java代碼?? 收藏代碼
  1. import ?java.io.FileOutputStream;??
  2. import ?java.io.IOException;??
  3. import ?java.io.OutputStream;??
  4. ??
  5. public ? class ?CharSetDemo?{??
  6. ???? /** ?
  7. ?????*?@description?字符編碼學(xué)習(xí)測(cè)試方法 ?
  8. ?????*?@param?args ?
  9. ?????*?@throws?IOException?文件IO異常處理 ?
  10. ?????*/ ??
  11. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  12. ???????? //?1、字符編碼,通過(guò)system類來(lái)獲取 ??
  13. ????????String?fe?=?System.getProperty( "file.encoding" );??
  14. ????????System.out.println(fe);? //?GBK ??
  15. ??????????
  16. ???????? //?2、進(jìn)行轉(zhuǎn)碼操作 ??
  17. ????????OutputStream?out?=? new ?FileOutputStream( "d:\\helloworld.txt" );??
  18. ???????? byte ?b[]?=? "Java,你好!!!" .getBytes( "ISO8859-1" );? //?轉(zhuǎn)碼操作 ??
  19. ????????out.write(b);? //?保存 ??
  20. ????????out.close();? //?關(guān)閉 ??
  21. ????}??
  22. }??

Java代碼?? 收藏代碼
  1. import ?java.io.FileInputStream;??
  2. import ?java.io.FileOutputStream;??
  3. import ?java.io.InputStream;??
  4. import ?java.io.ObjectInputStream;??
  5. import ?java.io.OutputStream;??
  6. import ?java.io.ObjectOutputStream;??
  7. import ?java.io.Serializable;??
  8. ??
  9. class ?Demo? implements ?Serializable{? //?實(shí)現(xiàn)序列化接口 ??
  10. ???? //?序列化方式之一,Java自身提供的序列化支持 ??
  11. ???? //?其他序列化方式,待以后有需要進(jìn)一步學(xué)習(xí) ??
  12. ???? private ? static ? final ? long ?serialVersionUID?=?1L;??
  13. ???? private ?String?info;? //?定義私有屬性 ??
  14. ??????
  15. ???? //?如果某個(gè)屬性不想被序列化,采用transient來(lái)標(biāo)識(shí) ??
  16. ???? //private?transient?String?noser;? ??
  17. ??????
  18. ???? public ?Demo(String?info){??
  19. ???????? this .info?=?info;??
  20. ????}??
  21. ???? public ?String?getInfo()?{??
  22. ???????? return ?info;??
  23. ????}??
  24. ???? public ? void ?setInfo(String?info)?{??
  25. ???????? this .info?=?info;??
  26. ????}??
  27. ???? public ?String?toString()?{??
  28. ???????? return ? "info?=?" ?+? this .info;??
  29. ????}??
  30. }??
  31. public ? class ?SerializedDemo?{??
  32. ???? /** ?
  33. ?????*@description?對(duì)象序列化、反序列化操作 ?
  34. ?????*@param?args ?
  35. ?????*@throws?Exception ?
  36. ?????*/ ??
  37. ???? public ? static ? void ?main(String[]?args)? throws ?Exception?{??
  38. ???????? //?1、對(duì)象序列化是將內(nèi)存中的對(duì)象轉(zhuǎn)換成二進(jìn)制形式的數(shù)據(jù)流 ??
  39. ???????? //?2、對(duì)象反序列化剛好和對(duì)象序列化方向相反,將二進(jìn)制數(shù)據(jù)流轉(zhuǎn)換成對(duì)象 ??
  40. ??????????
  41. ???????? //?a、對(duì)象序列化采用ObjectOutputStream類 ??
  42. ????????ObjectOutputStream?oos?=? null ;? //?聲明對(duì)象輸出流 ??
  43. ????????OutputStream?out?=? new ?FileOutputStream( "d:\\helloworld.txt" );???
  44. ????????oos?=? new ?ObjectOutputStream(out);??
  45. ????????oos.writeObject( new ?Demo( "helloworld" ));??
  46. ????????oos.close();??
  47. ??????????
  48. ???????? //?b、對(duì)象反序列化采用ObjectInputStream類 ??
  49. ????????ObjectInputStream?ois?=? null ;? //?聲明對(duì)象輸入流 ??
  50. ????????InputStream?input?=? new ?FileInputStream( "d:\\helloworld.txt" );??
  51. ????????ois?=? new ?ObjectInputStream(input);? //?實(shí)例化對(duì)象輸入流 ??
  52. ????????Object?obj?=?ois.readObject();? //?讀取對(duì)象 ??
  53. ????????ois.close();??
  54. ????????System.out.println(obj);??
  55. ????}??
  56. }??

二、Java類集合框架?
在百度搜到的一個(gè)比較詳細(xì)的java類集合筆記,鏈接如下:?
http://wenku.baidu.com/view/52cf133f5727a5e9856a6186.html ?
最后,記錄一個(gè)思考題目:采用Java類集合如何實(shí)現(xiàn)一個(gè)stack,使得增加、刪除、獲取最大值、最小值以及中值的時(shí)間操作效率相當(dāng)。?
Java代碼?? 收藏代碼
  1. /** ?
  2. ?*?@author?Administrator ?
  3. ?*? ?
  4. ?*?@description?如何從java類集框架中選取適當(dāng)?shù)臄?shù)據(jù)結(jié)構(gòu)呢??? ?
  5. ?*?@history ?
  6. ?*/ ??
  7. public ? interface ?Stack<T>?{??
  8. ???? public ? void ?add(T?t);? //?添加元素 ??
  9. ???? public ? void ?delete(T?t);? //?刪除元素 ??
  10. ???? public ?T?getMax();? //?獲取最大值 ??
  11. ???? public ?T?getMin();? //?獲取最小值 ??
  12. ???? public ?T?getMiddle();? //?獲取第中間大值 ??
  13. } ?
<!--EndFragment-->

一、Java IO 編程


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 日韩国产精品视频 | 久久手机视频 | 亚洲国产高清视频在线观看 | 久久视频国产 | 老子影院午夜理伦手机不卡 | 久久久综合中文字幕久久 | 欧美成人精品欧美一级乱黄 | 99re这里只有热视频 | 国产成人亚洲影视在线 | 视频大全在线观看免费 | 黄频网站在线观看视频 | 中国欧美日韩一区二区三区 | 91成人免费观看网站 | 久操视频在线免费观看 | 一本岛高清v不卡免费一三区 | 久久九九国产 | 国产精品高清在线观看 | 91精品一区二区三区久久久久 | 五月月色开心婷婷久久合 | 国产小呦| 欧美日韩加勒比一区二区三区 | 欧美视频在线观在线看 | 天天天天天天操 | 免费福利在线播放 | 四虎国产精品永久在线网址 | 国内精品久久久久久麻豆 | 久久久精品免费 | 国产精品一区在线播放 | 午夜干b| 91精品国产欧美一区二区 | 国产99视频精品免费视频免里 | 午夜在线播放免费高清观看 | 亚洲国产精品久久久久666 | 四虎综合九九色九九综合色 | 成人在免费观看视频国产 | 手机看片自拍日韩日韩高清 | 亚洲精品国产成人7777 | 国产福利不卡视频在免费 | 久久精品美女久久 | 99精品国产一区二区三区 | 96精品视频在线播放免费观看 |