JAVA-IO详解-包括讲解IO中各种流及其用法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

JAVA学习重点知识         2012JAVA  IO总结    2012最新 MyLewis  JAVAᬍব⫳⌏1IO⌕ᇍ䈵㒻ᡓ݇㋏݊Ҫᐌ⫼Ϣ⌕᳝݇ⱘᇍ䈵˖ৡ⿄ᇍᑨⱘᇍ䈵᭛ӊ㉏Fileᠧॄ⌕PrintStreamPrintWriterㅵ䘧⌕PipedInputStreamPipedOutputStreamᑣ߫⌕SequenceInputStreamᇍ䈵ᑣ߫࣪⌕⌕ObjectInputStreamObjectOutputStream⌕ᄫヺ⌕ReaderBufferedReaderInputStreamReaderFileReaderWriterBufferedWriterOutputStreamWriterFileWriterᄫ㡖⌕InputStreamFileInputStreamFilterInputStreamBufferedInputStreamOutputStreamFileOutputStreamFilterOutputStreamBufferedOutputStreamJAVAᬍব⫳⌏2IO⌕ᄺд↩৥ϰ㓪ݭwangboakᭈ⧚IO⌕:⫼Ѣ໘⧚䆒໛Ϟⱘ᭄᥂DŽ䆒໛˖⹀Ⲭˈݙᄬˈ䬂ⲬᔩܹDŽIO᳝݋ԧⱘߚ㉏˖1ˈḍ᥂໘⧚ⱘ᭄᥂㉏ൟϡৠ˖ᄫ㡖⌕੠ᄫヺ⌕DŽ2ˈḍ᥂⌕৥ϡৠ˖䕧ܹ⌕੠䕧ߎ⌕DŽᄫヺ⌕ⱘ⬅ᴹ˖಴Ў᭛ӊ㓪ⷕⱘϡৠˈ㗠᳝њᇍᄫヺ䖯㸠催ᬜ᪡԰ⱘᄫヺ⌕ᇍ䈵DŽॳ⧚˖݊ᅲህᰃ෎Ѣᄫ㡖⌕䇏পᄫ㡖ᯊˈএᶹњᣛᅮⱘⷕ㸼DŽᄫ㡖⌕੠ᄫヺ⌕ⱘऎ߿˖1ˈᄫ㡖⌕䇏পⱘᯊ׭ˈ䇏ࠄϔϾᄫ㡖ህ䖨ಲϔϾᄫ㡖DŽᄫヺ⌕Փ⫼њᄫ㡖⌕䇏ࠄϔϾ៪໮Ͼᄫ㡖(Ё᭛ᇍᑨⱘᄫ㡖᭄ᰃϸϾˈ೼UTF-8ⷕ㸼Ёᰃ3Ͼᄫ㡖)ᯊDŽܜএᶹᣛᅮⱘ㓪ⷕ㸼ˈᇚᶹࠄⱘᄫヺ䖨ಲDŽ2ˈᄫ㡖⌕ৃҹ໘⧚᠔᳝㉏ൟ᭄᥂ˈབ೒⠛ˈmp3ˈaviDŽ㗠ᄫヺ⌕া㛑໘⧚ᄫヺ᭄᥂DŽ㒧䆎˖া㽕ᰃ໘⧚㒃᭛ᴀ᭄᥂ˈህ㽕Ӭܜ㗗㰥Փ⫼ᄫヺ⌕DŽ䰸ℸП໪䛑⫼ᄫ㡖⌕DŽIOⱘԧ㋏DŽ᠔݋໛ⱘ෎ᴀࡳ㛑ህ᳝ϸϾ˖䇏੠ݭDŽ1ˈᄫ㡖⌕InputStream˄䇏˅,OutputStream˄ݭ˅DŽ2ˈᄫヺ⌕˖Reader˄䇏˅ˈWriter˄ݭ˅DŽ෎ᴀⱘ䇏ݭ᪡԰ᮍᓣ˖಴Ў᭄᥂䗮ᐌ䛑ҹ᭛ӊᔶᓣᄬ೼DŽ᠔ҹህ㽕ᡒࠄIOԧ㋏Ёৃҹ⫼Ѣ᪡԰᭛ӊⱘ⌕ᇍ䈵DŽ䗮䖛ৡ⿄ৃҹ᳈ᆍᯧ㦋প䆹ᇍ䈵DŽ಴ЎIOԧ㋏Ёⱘᄤ㉏ৡৢ㓔㒱໻䚼ߚᰃ⠊㉏ৡ⿄DŽ㗠ࠡ㓔䛑ᰃԧ⦄ᄤ㉏ࡳ㛑ⱘৡᄫDŽJAVAᬍব⫳⌏3Reader|--InputStreamReader|--FileReader:ϧ䮼⫼Ѣ໘⧚᭛ӊⱘᄫヺ䇏প⌕ᇍ䈵DŽWriter|--OutputStreamWriter|--FileWriter:ϧ䮼⫼Ѣ໘⧚᭛ӊⱘᄫヺݭܹ⌕ᇍ䈵DŽReaderЁⱘᐌ㾕ⱘᮍ⊩˖1ˈintread()˖䇏পϔϾᄫヺDŽ䖨ಲⱘᰃ䇏ࠄⱘ䙷ϾᄫヺDŽབᵰ䇏ࠄ⌕ⱘ᳿ሒˈ䖨ಲ-1.2ˈintread(char[])˖ᇚ䇏ࠄⱘᄫヺᄬܹᣛᅮⱘ᭄㒘Ёˈ䖨ಲⱘᰃ䇏ࠄⱘᄫヺϾ᭄ˈгህᰃᕔ᭄㒘䞠㺙ⱘܗ㋴ⱘϾ᭄DŽབᵰ䇏ࠄ⌕ⱘ᳿ሒˈ䖨ಲ-1.3ˈclose():䇏পᄫヺ݊ᅲ⫼ⱘᰃwindow㋏㒳ⱘࡳ㛑ˈህᏠᳯՓ⫼ᅠ↩ৢˈ䖯㸠䌘⑤ⱘ䞞ᬒDŽWriterЁⱘᐌ㾕ⱘᮍ⊩˖1ˈwrite(ch):ᇚϔϾᄫヺݭܹࠄ⌕ЁDŽ2ˈwrite(char[]):ᇚϔϾᄫヺ᭄㒘ݭܹࠄ⌕ЁDŽ3ˈwrite(String):ᇚϔϾᄫヺІݭܹࠄ⌕ЁDŽ4ˈflush():ࠋᮄ⌕ˈᇚ⌕Ёⱘ᭄᥂ࠋᮄࠄⳂⱘഄЁˈ⌕䖬ᄬ೼DŽ5ˈclose():݇䯁䌘⑤˖೼݇䯁ࠡӮܜ䇗⫼flush()ˈࠋᮄ⌕Ёⱘ᭄᥂এⳂⱘഄDŽ✊⌕݇䯁DŽFileWriter:䆹㉏≵᳝⡍᳝ⱘᮍ⊩DŽা᳝㞾Ꮕⱘᵘ䗴ߑ᭄DŽ䆹㉏⡍⚍೼Ѣˈ1ˈ⫼Ѣ໘⧚᭛ᴀ᭛ӊDŽ2ˈ䆹㉏Ё᳝咬䅸ⱘ㓪ⷕ㸼ˈ3ˈ䆹㉏Ё᳝Јᯊ㓧ކDŽᵘ䗴ߑ᭄˖೼ݭܹ⌕ᇍ䈵߱ྟ࣪ᯊˈᖙ乏㽕᳝ϔϾᄬټ᭄᥂ⱘⳂⱘഄDŽFileWriter(Stringfilename):䆹ᵘ䗴ߑ᭄خњҔМџᚙਸ਼˛1ˈ䇗⫼㋏㒳䌘⑤DŽ2ˈ೼ᣛᅮԡ㕂ˈ߯ᓎϔϾ᭛ӊDŽ⊼ᛣ˖བᵰ䆹᭛ӊᏆᄬ೼ˈᇚӮ㹿㽚ⲪDŽFileWriter(Stringfilename,booleanappend):䆹ᵘ䗴ߑ᭄˖ᔧӴܹⱘboolean㉏ൟؐЎtrueᯊˈӮ೼ᣛᅮ᭛ӊ᳿ሒ໘䖯㸠᭄᥂ⱘ㓁ݭDŽJAVAᬍব⫳⌏4FileReader˖1ˈ⫼Ѣ䇏প᭛ᴀ᭛ӊⱘ⌕ᇍ䈵DŽ2ˈ⫼Ѣ݇㘨᭛ᴀ᭛ӊDŽᵘ䗴ߑ᭄˖೼䇏প⌕ᇍ䈵߱ྟ࣪ⱘᯊ׭ˈᖙ乏㽕ᣛᅮϔϾ㹿䇏পⱘ᭛ӊDŽབᵰ䆹᭛ӊϡᄬ೼Ӯথ⫳FileNotFoundException.FileReader(Stringfilename);⏙ऩ1˖1,ᇚ᭛ᴀ᭄᥂ᄬټࠄϔϾ᭛ӊЁDŽclassDemo{publicstaticvoidmain(String[]args)throwsIOException{FileWriterfw=newFileWrier(demo.txt);fw.write(abcdec);fw.flush();fw.write(kkkk);fw.close();}}ᇍѢ䇏প៪㗙ݭܹ⌕ᇍ䈵ⱘᵘ䗴ߑ᭄ˈҹঞ䇏ݭᮍ⊩ˈ䖬᳝ࠋᮄ݇䯁ࡳ㛑䛑ӮᡯߎIOException៪݊ᄤ㉏DŽ᠔ҹ䛑㽕䖯㸠໘⧚DŽ៪㗙throwsᡯߎˈ៪㗙trycatch໘⧚DŽ⏙ऩ2˖ᅠᭈⱘᓖᐌ໘⧚ᮍᓣDŽclassDemo{publicstaticvoidmain(String[]args){FileWriterfw=null;try{fw=newFileWrier(z:\\demo.txt);fw.write(abcdec);JAVAᬍব⫳⌏5fw.flush();fw.write(kkkk);}catch(IOExceptione){System.out.println(e.toString());}finally{if(fw!=null)try{fw.close();}catch(IOExceptione){System.out.println(close:+e.toString());}}}}঺ϔϾᇣ㒚㡖˖ᔧᣛᅮ㒱ᇍ䏃ᕘᯊˈᅮНⳂᔩߚ䱨ヺ᳝ϸ⾡ᮍᓣ˖1ˈড᭰㒓Ԛᰃϔᅮ㽕ݭϸϾDŽ\\newFileWriter(c:\\demo.txt);2ˈ᭰㒓/ݭϔϾेৃDŽnewFileWriter(c:/demo.txt);⏙ऩ3˖䇏পϔϾᏆ᳝ⱘ᭛ᴀ᭛ӊˈᇚ᭛ᴀ᭄᥂ᠧॄߎᴹDŽϔ⃵䇏ϔϾᄫヺህᠧॄߎᴹˈᬜ⥛ϡ催DŽclassDemo{publicstaticvoidmain(String[]args)throwsIOException{FileReaderfr=newFileReader(demo.txt);intch=0;//ϔ⃵䇏ϔϾᄫヺDŽJAVAᬍব⫳⌏6while((ch=fr.read())!=-1){System.out.print((char)ch);}fr.close();}}䇏ϔϾᄫヺህᄬܹᄫヺ᭄㒘䞠ˈ䇏ᅠ1KbݡᠧॄDŽclassDemo{publicstaticvoidmain(String[]args){FileReaderfr=null;try{fr=newFileReader(demo.txt);char[]buf=newchar[1024];//䆹䭓ᑺ䗮ᐌ䛑ᰃ1024ⱘᭈ᭄סDŽintlen=0;while((len=fr.read(buf))!=-1){System.out.println(newString(buf,0,len));}}catch(IOExceptione){System.out.println(e.toString());}finally{if(fr!=null)try{fr.close();}catch(IOExceptione){JAVAᬍব⫳⌏7System.out.println(close:+e.toString());}}}}ᄫヺ⌕ⱘ㓧ކऎ˖㓧ކऎⱘߎ⦄ᦤ催њᇍ⌕ⱘ᪡԰ᬜ⥛DŽॳ⧚˖݊ᅲህᰃᇚ᭄㒘䖯㸠ᇕ㺙DŽᇍᑨⱘᇍ䈵˖BufferedWriter˖⡍᳝ᮍ⊩˖newLine()˖䎼ᑇৄⱘᤶ㸠ヺDŽBufferedReader˖⡍᳝ᮍ⊩˖readLine():ϔ⃵䇏ϔ㸠ˈࠄ㸠ᷛ䆄ᯊˈᇚ㸠ᷛ䆄Пࠡⱘᄫヺ᭄᥂԰ЎᄫヺІ䖨ಲDŽᔧ䇏ࠄ᳿ሒᯊˈ䖨ಲnullDŽ೼Փ⫼㓧ކऎᇍ䈵ᯊˈ㽕ᯢ⹂ˈ㓧ކⱘᄬ೼ᰃЎњ๲ᔎ⌕ⱘࡳ㛑㗠ᄬ೼ˈ᠔ҹ೼ᓎゟ㓧ކऎᇍ䈵ᯊˈ㽕ܜ᳝⌕ᇍ䈵ᄬ೼DŽ݊ᅲ㓧ކݙ䚼ህᰃ೼Փ⫼⌕ᇍ䈵ⱘᮍ⊩ˈাϡ䖛ࡴܹњ᭄㒘ᇍ᭄᥂䖯㸠њЈᯊᄬټDŽЎњᦤ催᪡԰᭄᥂ⱘᬜ⥛DŽҷⷕϞⱘԧ⦄˖ݭܹ㓧ކऎᇍ䈵DŽ//ᓎゟ㓧ކऎᇍ䈵ᖙ乏ᡞ⌕ᇍ䈵԰Ўখ᭄Ӵ䗦㒭㓧ކऎⱘᵘ䗴ߑ᭄DŽBufferedWriterbufw=newBufferedWriter(newFileWriter(buf.txt));bufw.write(abce);//ᇚ᭄᥂ݭܹࠄњ㓧ކऎDŽbufw.flush();//ᇍ㓧ކऎⱘ᭄᥂䖯㸠ࠋᮄDŽᇚ᭄᥂ࠋࠄⳂⱘഄЁDŽbufw.close();//݇䯁㓧ކऎˈ݊ᅲ݇䯁ⱘᰃ㹿ࣙ㺙೼ݙ䚼ⱘ⌕ᇍ䈵DŽJAVAᬍব⫳⌏8䇏প㓧ކऎᇍ䈵DŽBufferedReaderbufr=newBufferedReader(newFileReader(buf.txt));Stringline=null;//ᣝ✻㸠ⱘᔶᓣপߎ᭄᥂DŽপߎⱘ↣ϔϾ㸠᭄᥂ϡࣙ৿ಲ䔺ヺDŽwhile((line=bufr.readLine())!=null){System.out.println(line);}bufr.close();㒗д˖䗮䖛㓧ކऎⱘᔶᓣˈᇍ᭛ᴀ᭛ӊ䖯㸠ᣋ䋱DŽpublicstaticvoidmain(String[]args){BufferedReaderbufr=newBufferedReader(newFileReader(a.txt));BufferedWriterbufw=newBufferedWriter(newFileWriter(b.txt));Stringline=null;while((line=bufr.readLine())!=null){bufw.write(line);bufw.newLine();bufw.flush();}bufw.close();bufr.close();}readLine():ᮍ⊩ⱘॳ⧚˖݊ᅲ㓧ކऎЁⱘ䆹ᮍ⊩ˈ⫼ⱘ䖬ᰃϢ㓧ކऎ݇㘨ⱘ⌕ᇍ䈵ⱘreadᮍ⊩DŽাϡ䖛ˈ↣ϔ⃵䇏ࠄϔϾᄫヺˈܜϡ䖯㸠݋ԧ᪡԰ˈܜ䖯㸠ЈᯊᄬټDŽᔧ䇏পࠄಲ䔺ᷛ䆄ᯊˈᇚЈᯊᆍ఼Ёᄬټⱘ᭄᥂ϔ⃵ᗻ䖨ಲDŽ᮶✊ᯢ⹂њॳ⧚ˈ៥Ӏгৃҹᅲ⦄ϔϾ㉏Ԑࡳ㛑ⱘᮍ⊩DŽJAVAᬍব⫳⌏9classMyBufferedReader{privateReaderr;MyBufferedReader(Readerr){this.r=r;}publicStringmyReadLine()throwsIOException{//1,߯ᓎЈᯊᆍ఼DŽStringBuildersb=newStringBuilder();//2,ᕾ⦃ⱘՓ⫼readᮍ⊩ϡᮁ䇏পᄫヺDŽintch=0;while((ch=r.read())!=-1){if(ch=='\r')continue;if(ch=='\n')returnsb.toString();elsesb.append((char)ch);}if(sb.length()!=0)returnsb.toString();returnnull;}publicvoidmyClose()throwsIOException{r.close();}}main(){MyBufferedReadermyBufr=newMyBufferedReader(newFileReader(a.txt));Stringline=null;while((line=myBufr.myReadLine())!=null){System.out.println(line);}}JAVAᬍব⫳⌏10ᅗⱘߎ⦄෎Ѣ⌕ᑊ๲ᔎњ⌕ⱘࡳ㛑DŽ䖭гᰃϔ⾡䆒䅵῵ᓣⱘԧ⦄˖㺙㺙佄䆒䅵῵ᓣDŽᇍϔ㒘ᇍ䈵䖯㸠ࡳ㛑ⱘ๲ᔎDŽ䆹῵ᓣ੠㒻ᡓ᳝ҔМऎ߿ਸ਼˛ᅗ↨㒻ᡓ᳝᳈དⱘ♉⌏ᗻDŽ䗮ᐌ㺙佄㉏੠㹿㺙佄㉏䛑ৠሲϢϔϾ⠊㉏៪㗙᥹ষDŽWriter|--MediaWriter|-

1 / 30
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功