欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

一、Java IO 編程

系統 1929 0

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

Java代碼?? 收藏代碼
  1. import ?java.io.File;??
  2. import ?java.io.IOException;??
  3. ??
  4. public ? class ?FileDemoTest?{??
  5. ???? /** ?
  6. ?????*?@description?基礎File類操作 ?
  7. ?????*?@param?args ?
  8. ?????*?@throws?IOException?IO異常處理 ?
  9. ?????*/ ??
  10. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  11. ???????? //?Windows系統下的文件目錄格式,這個和Unix系統等不同 ??
  12. ???????? //?String?pathSeparator?=?File.pathSeparator;?//?; ??
  13. ???????? //?String?separator?=?File.separator;?//?\ ??
  14. ??
  15. ???????? //?1、創建和刪除文件 ??
  16. ????????File?file?=? new ?File( "d:\\helloworld.txt" );??
  17. ???????? //?File?file?=?new?File("d:"+File.separator+"helloworld.txt"); ??
  18. ????????file.createNewFile();? //?創建文件 ??
  19. ???????? if ?(file.exists())?{? //?如果文件存在,則刪除文件 ??
  20. ????????????file.delete();??
  21. ????????}??
  22. ??
  23. ???????? //?2、創建文件夾操作 ??
  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();? //?列出全部內容 ??
  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?字節流:輸入流InputStream ?
  9. ?????*?@param?args ?
  10. ?????*?@throws?IOException ?
  11. ?????*/ ??
  12. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  13. ???????? //?字節流的輸入流和輸出流過程步驟比較固定 ??
  14. ???????? //?第1步、使用File類找到一個文件 ??
  15. ????????File?f?=? new ?File( "d:" ?+?File.separator?+? "helloworld.txt" );??
  16. ???????? //?第2步、通過子類實例化父類對象(InputStream為抽象類,本身不能直接實例化) ??
  17. ????????InputStream?input?=? new ?FileInputStream(f);??
  18. ???????? //?第3步、進行讀操作 ??
  19. ???????? byte ?b[]?=? new ? byte [( int )f.length()];? //?數組大小由文件大小來確定 ??
  20. ???????? for ?( int ?i?=? 0 ;?i?<?b.length;?i++)?{??
  21. ????????????b[i]?=?( byte )?input.read();? //?讀取內容 ??
  22. ????????}??
  23. ???????? //?第4步、關閉輸出流 ??
  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?字節流:輸出流OutputStream類 ?
  9. ?????*@param?args ?
  10. ?????*@throws?IOException? ?
  11. ?????*/ ??
  12. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  13. ???????? //?輸入和輸出流參考的是Java程序,輸出流操作步驟也比較固定 ??
  14. ???????? //?第1步、使用File類找到一個文件 ??
  15. ????????File?f?=? new ?File( "d:" ?+?File.separator?+? "helloworld.txt" );??
  16. ???????? //?第2步、通過子類實例化父類對象 ??
  17. ???????? //OutputStream?out?=?new?FileOutputStream(f); ??
  18. ????????OutputStream?out?=? new ?FileOutputStream(f, true );??
  19. ???????? //?第3步、進行寫操作 ??
  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]);? //?單個寫入 ??
  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步、關閉輸出流 ??
  30. ????????out.close();? //?關閉輸出流 ??
  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?字節流和字符流按照處理數據的單位劃分 ?
  9. ?????*@param?args ?
  10. ?????*@throws?IOException?IO異常處理 ?
  11. ?????*/ ??
  12. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  13. ???????? //?第1步、使用File類找到一個文件 ??
  14. ????????File?f?=? new ?File( "d:" ?+?File.separator?+? "helloworld.txt" );??
  15. ???????? //?第2步、通過子類實例化父類對象 ??
  16. ????????Reader?input?=? new ?FileReader(f);??
  17. ???????? //?第3步、進行讀操作 ??
  18. ???????? char ?c[]?=? new ? char [ 1024 ];??
  19. ???????? int ?temp?=? 0 ;??
  20. ???????? int ?len?=? 0 ;??
  21. ???????? while ?((temp?=?input.read())?!=?- 1 )?{??
  22. ???????????? //?如果不是-1就表示還有內容,可以繼續讀取 ??
  23. ????????????c[len]?=?( char )?temp;??
  24. ????????????len++;??
  25. ????????}??
  26. ???????? //?第4步、關閉輸出流 ??
  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?通過代碼學習發現,基本上步驟一致的 ?
  9. ?????*@param?args ?
  10. ?????*@throws?IOException ?
  11. ?????*/ ??
  12. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  13. ???????? //?第1步、使用File類找到一個文件 ??
  14. ????????File?f?=? new ?File( "d:" ?+?File.separator?+? "helloworld.txt" );??
  15. ???????? //?第2步、通過子類實例化父類對象 ??
  16. ????????Writer?out?=? new ?FileWriter(f);? //?通過對象多態性,進行實例化 ??
  17. ???????? //?第3步、進行寫操作 ??
  18. ????????String?str?=? "\nsay?hello\tworld" ;???
  19. ????????out.write(str);???
  20. ???????? //?第4步、關閉輸出流 ??
  21. ???????? //?out.flush();?//?清空緩沖 ??
  22. ????????out.close();? //?關閉輸出流 ??
  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??字節流:InputStream,OutputStream ?
  15. ?????*??????????????字符流:Reader,Writer?? ?
  16. ?????*??????????????//?字節流和字符流相互轉換測試 ?
  17. ?????*@param?args ?
  18. ?????*?@throws?IOException?文件操作異常處理 ?
  19. ?????*/ ??
  20. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  21. ???????? //?1、輸入流-字節流轉換成字符流 ??
  22. ???????? //?通過InputStreamReader類來進行轉換 ??
  23. ????????InputStream?is?=? new ?FileInputStream( "d:\\helloworld.txt" );??
  24. ????????Reader?reader?=? new ?InputStreamReader(is);? //?將字節流轉換成字符流 ??
  25. ???????? char ?c[]?=? new ? char [ 1024 ];??
  26. ???????? int ?len?=?reader.read(c);? //?讀取操作,保存在字符數組中 ??
  27. ????????reader.close();? //?關閉操作 ??
  28. ????????System.out.println( new ?String(c, 0 ,len));? //?字符數組轉換成String類實例 ??
  29. ??????????
  30. ???????? //?2、輸出流-字節流轉換成字符流 ??
  31. ???????? //?通過OutputStreamWriter類來進行轉換 ??
  32. ????????File?f?=? new ?File( "d:" ?+?File.separator?+? "helloworld.txt" );??
  33. ????????Writer?out?=? new ?OutputStreamWriter( new ?FileOutputStream(f));? //?字節流變為字符流 ??
  34. ????????out.write( "hello?world!!!" );? //?使用字符流輸出 ??
  35. ????????out.close();??
  36. ??????????
  37. ???????? //?3、從字符流到字節流轉換可以采用String類提供的操作 ??
  38. ???????? //?從字符流中獲取char[],轉換成String實例然后再調用String?API的getBytes()方法? ??
  39. ???????? //?接1中的代碼如下,最后通過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、其他常見的流 ??
  46. ???????? //?內存操作流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?字符編碼學習測試方法 ?
  8. ?????*?@param?args ?
  9. ?????*?@throws?IOException?文件IO異常處理 ?
  10. ?????*/ ??
  11. ???? public ? static ? void ?main(String[]?args)? throws ?IOException?{??
  12. ???????? //?1、字符編碼,通過system類來獲取 ??
  13. ????????String?fe?=?System.getProperty( "file.encoding" );??
  14. ????????System.out.println(fe);? //?GBK ??
  15. ??????????
  16. ???????? //?2、進行轉碼操作 ??
  17. ????????OutputStream?out?=? new ?FileOutputStream( "d:\\helloworld.txt" );??
  18. ???????? byte ?b[]?=? "Java,你好?。?!" .getBytes( "ISO8859-1" );? //?轉碼操作 ??
  19. ????????out.write(b);? //?保存 ??
  20. ????????out.close();? //?關閉 ??
  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{? //?實現序列化接口 ??
  10. ???? //?序列化方式之一,Java自身提供的序列化支持 ??
  11. ???? //?其他序列化方式,待以后有需要進一步學習 ??
  12. ???? private ? static ? final ? long ?serialVersionUID?=?1L;??
  13. ???? private ?String?info;? //?定義私有屬性 ??
  14. ??????
  15. ???? //?如果某個屬性不想被序列化,采用transient來標識 ??
  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?對象序列化、反序列化操作 ?
  34. ?????*@param?args ?
  35. ?????*@throws?Exception ?
  36. ?????*/ ??
  37. ???? public ? static ? void ?main(String[]?args)? throws ?Exception?{??
  38. ???????? //?1、對象序列化是將內存中的對象轉換成二進制形式的數據流 ??
  39. ???????? //?2、對象反序列化剛好和對象序列化方向相反,將二進制數據流轉換成對象 ??
  40. ??????????
  41. ???????? //?a、對象序列化采用ObjectOutputStream類 ??
  42. ????????ObjectOutputStream?oos?=? null ;? //?聲明對象輸出流 ??
  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、對象反序列化采用ObjectInputStream類 ??
  49. ????????ObjectInputStream?ois?=? null ;? //?聲明對象輸入流 ??
  50. ????????InputStream?input?=? new ?FileInputStream( "d:\\helloworld.txt" );??
  51. ????????ois?=? new ?ObjectInputStream(input);? //?實例化對象輸入流 ??
  52. ????????Object?obj?=?ois.readObject();? //?讀取對象 ??
  53. ????????ois.close();??
  54. ????????System.out.println(obj);??
  55. ????}??
  56. }??

二、Java類集合框架?
在百度搜到的一個比較詳細的java類集合筆記,鏈接如下:?
http://wenku.baidu.com/view/52cf133f5727a5e9856a6186.html ?
最后,記錄一個思考題目:采用Java類集合如何實現一個stack,使得增加、刪除、獲取最大值、最小值以及中值的時間操作效率相當。?
Java代碼?? 收藏代碼
  1. /** ?
  2. ?*?@author?Administrator ?
  3. ?*? ?
  4. ?*?@description?如何從java類集框架中選取適當的數據結構呢??? ?
  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 編程


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 色综合天天综合中文网 | 色射综合| 久久国内精品 | 色爱综合区五月小说 | 日韩在线观看视频一区二区三区 | www.天天操| 久热官网 | 欧美人成在线视频 | 国产区免费在线观看 | 五月婷久久 | 亚洲高清视频在线观看 | 精品一区二区三区在线观看视频 | 国产精品久久久久久久久久日本 | 久久av影院| 九色com| 亚洲性生活免费视频 | 国产欧美精品午夜在线播放 | 一本一道久久综合狠狠老 | 香港一级毛片 | 亚洲人一区 | 国产一级在线观看视频 | 免费在线国产视频 | 波多野结衣a∨免费观看 | 91看片淫黄大片欧美看国产片 | 日本青草视频 | 久久亚洲这里只有精品18 | 色悠久久久久综合欧美99 | 成人av免费在线观看 | 天天艹夜夜 | 国产一区在线免费观看 | 丝袜美腿视频一区二区三区 | 日韩美在线 | 热99re久久精品2久久久 | 免费福利视频在线观看 | 欧美激情综合色综合啪啪五月 | 全部三片在线观看直播 | 国产精品久久久久久免费 | 激情六月综合 | 久久伊人中文字幕有码 | 国产精品99999999| 唐人社电亚洲一区二区三区 |