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|-