Java IO 总结

wushuying

贡献于2015-07-06

字数:0 关键词: Java开发 Java

JAVA 学习重点知识                2012 JAVA  IO 总结     2012 最新  MyLewis    JAVA ᬍব ⫳⌏ 1 IO ⌕ᇍ䈵㒻ᡓ݇㋏ ݊Ҫᐌ⫼Ϣ⌕᳝݇ⱘᇍ䈵˖ ৡ⿄ ᇍᑨⱘᇍ䈵 ᭛ӊ㉏ File ᠧॄ⌕ PrintStream PrintWriter ㅵ䘧⌕ PipedInputStream PipedOutputStream ᑣ߫⌕ SequenceInputStream ᇍ䈵ᑣ߫࣪⌕⌕ ObjectInputStream ObjectOutputStream ⌕ ᄫヺ⌕ Reader BufferedReader InputStreamReader FileReader Writer BufferedWriter OutputStreamWriter FileWriter ᄫ㡖⌕ InputStream FileInputStream FilterInputStream BufferedInputStream OutputStream FileOutputStream FilterOutputStream BufferedOutputStream ಴Ў IO ԧ㋏Ёⱘᄤ㉏ৡৢ㓔㒱໻䚼ߚᰃ⠊㉏ৡ⿄DŽ㗠ࠡ㓔䛑ᰃԧ⦄ᄤ㉏ࡳ㛑ⱘৡᄫDŽ 䗮䖛ৡ⿄ৃҹ᳈ᆍᯧ㦋প䆹ᇍ䈵DŽ ᠔ҹህ㽕ᡒࠄ IO ԧ㋏Ёৃҹ⫼Ѣ᪡԰᭛ӊⱘ⌕ᇍ䈵DŽ ಴Ў᭄᥂䗮ᐌ䛑ҹ᭛ӊᔶᓣᄬ೼DŽ ෎ᴀⱘ䇏ݭ᪡԰ᮍᓣ˖ Reader˄䇏˅ˈWriter˄ݭ˅DŽ 2ˈᄫヺ⌕˖ InputStream˄䇏˅,OutputStream˄ݭ˅DŽ 1ˈᄫ㡖⌕ IO ⱘԧ㋏DŽ᠔݋໛ⱘ෎ᴀࡳ㛑ህ᳝ϸϾ˖䇏 ੠ ݭDŽ 㗗㰥Փ⫼ᄫヺ⌕DŽ䰸ℸП໪䛑⫼ᄫ㡖⌕DŽܜ 㒧䆎˖া㽕ᰃ໘⧚㒃᭛ᴀ᭄᥂ˈህ㽕Ӭ 㗠ᄫヺ⌕া㛑໘⧚ᄫヺ᭄᥂DŽ 2ˈᄫ㡖⌕ৃҹ໘⧚᠔᳝㉏ൟ᭄᥂ˈབ೒⠛ˈmp3ˈaviDŽ এᶹᣛᅮⱘ㓪ⷕ㸼ˈᇚᶹࠄⱘᄫヺ䖨ಲDŽܜ ⷕ㸼Ёᰃ 3 Ͼᄫ㡖)ᯊDŽ ᄫヺ⌕Փ⫼њᄫ㡖⌕䇏ࠄϔϾ៪໮Ͼᄫ㡖(Ё᭛ᇍᑨⱘᄫ㡖᭄ᰃϸϾˈ೼ UTF-8 1ˈᄫ㡖⌕䇏পⱘᯊ׭ˈ䇏ࠄϔϾᄫ㡖ህ䖨ಲϔϾᄫ㡖DŽ ᄫ㡖⌕੠ᄫヺ⌕ⱘऎ߿˖ ॳ⧚˖݊ᅲህᰃ෎Ѣᄫ㡖⌕䇏পᄫ㡖ᯊˈএᶹњᣛᅮⱘⷕ㸼DŽ ಴Ў᭛ӊ㓪ⷕⱘϡৠˈ㗠᳝њᇍᄫヺ䖯㸠催ᬜ᪡԰ⱘᄫヺ⌕ᇍ䈵DŽ ᄫヺ⌕ⱘ⬅ᴹ˖ 2ˈḍ᥂⌕৥ϡৠ˖䕧ܹ⌕੠䕧ߎ⌕DŽ 1ˈḍ᥂໘⧚ⱘ᭄᥂㉏ൟϡৠ˖ᄫ㡖⌕੠ᄫヺ⌕DŽ IO ᳝݋ԧⱘߚ㉏˖ 䆒໛˖⹀Ⲭˈݙᄬˈ䬂ⲬᔩܹDŽ IO ⌕:⫼Ѣ໘⧚䆒໛Ϟⱘ᭄᥂DŽ wangboak ᭈ⧚ ↩৥ϰ 㓪ݭ IO ⌕ ᄺд 2 JAVA ᬍব ⫳⌏ ᥂ⱘ㓁ݭDŽ 䆹ᵘ䗴ߑ᭄˖ᔧӴܹⱘ boolean ㉏ൟؐЎ true ᯊˈӮ೼ᣛᅮ᭛ӊ᳿ሒ໘䖯㸠᭄ FileWriter(String filename,boolean append): ⊼ᛣ˖བᵰ䆹᭛ӊᏆᄬ೼ˈᇚӮ㹿㽚ⲪDŽ 2ˈ೼ᣛᅮԡ㕂ˈ߯ᓎϔϾ᭛ӊDŽ 1ˈ䇗⫼㋏㒳䌘⑤DŽ њҔМџᚙਸ਼˛خ 䆹ᵘ䗴ߑ᭄ FileWriter(String filename): ᭄᥂ⱘⳂⱘഄDŽټ ᵘ䗴ߑ᭄˖೼ݭܹ⌕ᇍ䈵߱ྟ࣪ᯊˈᖙ乏㽕᳝ϔϾᄬ DŽކ 3ˈ䆹㉏Ё᳝Јᯊ㓧 2ˈ䆹㉏Ё᳝咬䅸ⱘ㓪ⷕ㸼ˈ 1ˈ⫼Ѣ໘⧚᭛ᴀ᭛ӊDŽ 䆹㉏⡍⚍೼Ѣˈ 䆹㉏≵᳝⡍᳝ⱘᮍ⊩DŽা᳝㞾Ꮕⱘᵘ䗴ߑ᭄DŽ FileWriter: ݇䯁DŽ 䇗⫼ flush()ˈࠋᮄ⌕Ёⱘ᭄᥂এⳂⱘഄDŽ✊⌕ܜ 5ˈclose():݇䯁䌘⑤˖೼݇䯁ࠡӮ 4ˈflush():ࠋᮄ⌕ˈᇚ⌕Ёⱘ᭄᥂ࠋᮄࠄⳂⱘഄЁˈ⌕䖬ᄬ೼DŽ 3ˈwrite(String): ᇚϔϾᄫヺІݭܹࠄ⌕ЁDŽ 2ˈwrite(char[]): ᇚϔϾᄫヺ᭄㒘ݭܹࠄ⌕ЁDŽ 1ˈwrite(ch): ᇚϔϾᄫヺݭܹࠄ⌕ЁDŽ Writer Ёⱘᐌ㾕ⱘᮍ⊩˖ ᬒDŽ 䇏পᄫヺ݊ᅲ⫼ⱘᰃ window ㋏㒳ⱘࡳ㛑ˈህᏠᳯՓ⫼ᅠ↩ৢˈ䖯㸠䌘⑤ⱘ䞞 3ˈclose(): ㋴ⱘϾ᭄DŽབᵰ䇏ࠄ⌕ⱘ᳿ሒˈ䖨ಲ-1.ܗ 㺙ⱘ ᇚ䇏ࠄⱘᄫヺᄬܹᣛᅮⱘ᭄㒘Ёˈ䖨ಲⱘᰃ䇏ࠄⱘᄫヺϾ᭄ˈгህᰃᕔ᭄㒘䞠 2ˈint read(char[])˖ 䇏পϔϾᄫヺDŽ䖨ಲⱘᰃ䇏ࠄⱘ䙷ϾᄫヺDŽབᵰ䇏ࠄ⌕ⱘ᳿ሒˈ䖨ಲ-1. 1ˈint read()˖ Reader Ёⱘᐌ㾕ⱘᮍ⊩˖ |--FileWriter:ϧ䮼⫼Ѣ໘⧚᭛ӊⱘᄫヺݭܹ⌕ᇍ䈵DŽ |--OutputStreamWriter Writer |--FileReader:ϧ䮼⫼Ѣ໘⧚᭛ӊⱘᄫヺ䇏প⌕ᇍ䈵DŽ |--InputStreamReader Reader 3 JAVA ᬍব ⫳⌏ ;(" fw.write("abcdec fw = new FileWrier("z:\\demo.txt"); { try FileWriter fw = null; { public static void main(String[] args) { class Demo ᅠᭈⱘᓖᐌ໘⧚ᮍᓣDŽ ⏙ऩ 2˖ ᠔ҹ䛑㽕䖯㸠໘⧚DŽ៪㗙 throws ᡯߎˈ៪㗙 try catch ໘⧚DŽ IOException ៪݊ᄤ㉏DŽ ᇍѢ䇏প៪㗙ݭܹ⌕ᇍ䈵ⱘᵘ䗴ߑ᭄ˈҹঞ䇏ݭᮍ⊩ˈ䖬᳝ࠋᮄ݇䯁ࡳ㛑䛑Ӯᡯߎ } } fw.close(); fw.write("kkkk"); fw.flush(); fw.write("abcdec"); FileWriter fw = new FileWrier("demo.txt"); { public static void main(String[] args) throws IOException { class Demo ࠄϔϾ᭛ӊЁDŽټ 1,ᇚ᭛ᴀ᭄᥂ᄬ ⏙ऩ 1˖ FileReader(String filename); བᵰ䆹᭛ӊϡᄬ೼Ӯথ⫳ FileNotFoundException. ᵘ䗴ߑ᭄˖೼䇏প⌕ᇍ䈵߱ྟ࣪ⱘᯊ׭ˈᖙ乏㽕ᣛᅮϔϾ㹿䇏পⱘ᭛ӊDŽ 2ˈ⫼Ѣ݇㘨᭛ᴀ᭛ӊDŽ 1ˈ⫼Ѣ䇏প᭛ᴀ᭛ӊⱘ⌕ᇍ䈵DŽ FileReader˖ 4 JAVA ᬍব ⫳⌏ //ϔ⃵䇏ϔϾᄫヺDŽ int ch = 0; FileReader fr = new FileReader("demo.txt"); { public static void main(String[] args) throws IOException { class Demo ϔ⃵䇏ϔϾᄫヺህᠧॄߎᴹˈᬜ⥛ϡ催DŽ 䇏পϔϾᏆ᳝ⱘ᭛ᴀ᭛ӊˈᇚ᭛ᴀ᭄᥂ᠧॄߎᴹDŽ ⏙ऩ 3˖ 2ˈ᭰㒓 / ݭϔϾेৃDŽ new FileWriter("c:/demo.txt"); 1ˈড᭰㒓 Ԛᰃϔᅮ㽕ݭϸϾDŽ\\ new FileWriter("c:\\demo.txt"); ᔧᣛᅮ㒱ᇍ䏃ᕘᯊˈᅮНⳂᔩߚ䱨ヺ᳝ϸ⾡ᮍᓣ˖ ঺ϔϾᇣ㒚㡖˖ } } } } System.out.println("close:"+e.toString()); { catch(IOException e) } fw.close(); { try if(fw!=null) { finally } System.out.println(e.toString()); { catch(IOException e) } fw.write("kkkk"); fw.flush(); 5 JAVA ᬍব ⫳⌏ } ( catch(IOException e } fr.close(); { try if(fr!=null) { finally } System.out.println(e.toString()); { catch(IOException e) } } System.out.println(new String(buf,0,len)); { while((len=fr.read(buf))!=-1) int len = 0; DŽס char[] buf = new char[1024];//䆹䭓ᑺ䗮ᐌ䛑ᰃ 1024 ⱘᭈ᭄ fr = new FileReader("demo.txt"); { try FileReader fr = null; { public static void main(String[] args) { class Demo 䇏ϔϾᄫヺህᄬܹᄫヺ᭄㒘䞠ˈ䇏ᅠ 1Kb ݡᠧॄDŽ } } fr.close(); } System.out.print((char)ch); { while((ch=fr.read())!=-1) 6 JAVA ᬍব ⫳⌏ ऎˈ݊ᅲ݇䯁ⱘᰃ㹿ࣙ㺙೼ݙ䚼ⱘ⌕ᇍ䈵DŽކ bufw.close();//݇䯁㓧 ऎⱘ᭄᥂䖯㸠ࠋᮄDŽᇚ᭄᥂ࠋࠄⳂⱘഄЁDŽކ bufw.flush();//ᇍ㓧 ऎDŽކ bufw.write("abce");//ᇚ᭄᥂ݭܹࠄњ㓧 BufferedWriter bufw = new BufferedWriter(new FileWriter("buf.txt")); ऎⱘᵘ䗴ߑ᭄DŽކऎᇍ䈵ᖙ乏ᡞ⌕ᇍ䈵԰Ўখ᭄Ӵ䗦㒭㓧ކ //ᓎゟ㓧 ऎᇍ䈵DŽކ ݭܹ㓧 ҷⷕϞⱘԧ⦄˖ њᦤ催᪡԰᭄᥂ⱘᬜ⥛DŽ DŽЎټݙ䚼ህᰃ೼Փ⫼⌕ᇍ䈵ⱘᮍ⊩ˈাϡ䖛ࡴܹњ᭄㒘ᇍ᭄᥂䖯㸠њЈᯊᄬކ ݊ᅲ㓧 ᳝⌕ᇍ䈵ᄬ೼DŽܜऎᇍ䈵ᯊˈ㽕ކ ᠔ҹ೼ᓎゟ㓧 ⱘᄬ೼ᰃЎњ๲ᔎ⌕ⱘࡳ㛑㗠ᄬ೼ˈކऎᇍ䈵ᯊˈ㽕ᯢ⹂ˈ㓧ކ ೼Փ⫼㓧 ಲDŽᔧ䇏ࠄ᳿ሒᯊˈ䖨ಲ nullDŽ readLine():ϔ⃵䇏ϔ㸠ˈࠄ㸠ᷛ䆄ᯊˈᇚ㸠ᷛ䆄Пࠡⱘᄫヺ᭄᥂԰ЎᄫヺІ䖨 ⡍᳝ᮍ⊩˖ BufferedReader˖ newLine()˖䎼ᑇৄⱘᤶ㸠ヺDŽ ⡍᳝ᮍ⊩˖ BufferedWriter˖ ᇍᑨⱘᇍ䈵˖ ॳ⧚˖݊ᅲህᰃᇚ᭄㒘䖯㸠ᇕ㺙DŽ ऎⱘߎ⦄ᦤ催њᇍ⌕ⱘ᪡԰ᬜ⥛DŽކ 㓧 ऎ˖ކᄫヺ⌕ⱘ㓧 } } } } System.out.println("close:"+e.toString()); 7 JAVA ᬍব ⫳⌏ ᮶✊ᯢ⹂њॳ⧚ˈ៥Ӏгৃҹᅲ⦄ϔϾ㉏Ԑࡳ㛑ⱘᮍ⊩DŽ ⱘ᭄᥂ϔ⃵ᗻ䖨ಲDŽټ ᔧ䇏পࠄಲ䔺ᷛ䆄ᯊˈᇚЈᯊᆍ఼Ёᄬ DŽټ䖯㸠Јᯊᄬܜˈϡ䖯㸠݋ԧ᪡԰ܜˈ াϡ䖛ˈ↣ϔ⃵䇏ࠄϔϾᄫヺ ऎ݇㘨ⱘ⌕ᇍ䈵ⱘ read ᮍ⊩DŽކऎЁⱘ䆹ᮍ⊩ˈ⫼ⱘ䖬ᰃϢ㓧ކ ݊ᅲ㓧 readLine():ᮍ⊩ⱘॳ⧚˖ } bufr.close(); bufw.close(); } bufw.flush(); bufw.newLine(); bufw.write(line); { while((line=bufr.readLine())!=null) String line = null; BufferedWriter bufw = new BufferedWriter(new FileWriter("b.txt")); BufferedReader bufr = new BufferedReader(new FileReader("a.txt")); { public static void main(String[] args) ऎⱘᔶᓣˈᇍ᭛ᴀ᭛ӊ䖯㸠ᣋ䋱DŽކ 㒗д˖䗮䖛㓧 bufr.close(); } System.out.println(line); { while((line=bufr.readLine())!=null) //ᣝ✻㸠ⱘᔶᓣপߎ᭄᥂DŽপߎⱘ↣ϔϾ㸠᭄᥂ϡࣙ৿ಲ䔺ヺDŽ String line = null; BufferedReader bufr = new BufferedReader(new FileReader("buf.txt")); ऎᇍ䈵DŽކ 䇏প㓧 8 JAVA ᬍব ⫳⌏ JAVA ᬍব ⫳⌏ 9 class MyBufferedReader { private Reader r; MyBufferedReader(Reader r) { this.r = r; } public String myReadLine()throws IOException { //1,߯ᓎЈᯊᆍ఼DŽ StringBuilder sb = new StringBuilder(); //2,ᕾ⦃ⱘՓ⫼ read ᮍ⊩ϡᮁ䇏পᄫヺDŽ int ch = 0; while((ch=r.read())!=-1) { if(ch=='\r') continue; if(ch=='\n') return sb.toString(); else sb.append((char)ch); } if(sb.length()!=0) return sb.toString(); return null; } public void myClose()throws IOException { r.close(); } } main() { MyBufferedReader myBufr = new MyBufferedReader(new FileReader("a.txt")); String line = null; while((line=myBufr.myReadLine())!=null) { System.out.println(line); } } ᇚᇍ䖭ѯᇍ䈵⠊㉏ൟ䖯㸠᪡԰ेৃDŽ䖭ህᰃ໮ᗕˈᦤ催њ⿟ᑣⱘᠽሩᗻDŽ Ԛᰃᔧ᳝ᮄⱘᇍ䈵ߎ⦄ᯊˈ䖬㽕㒻㓁೼䆹㉏Ё⏏ࡴᵘ䗴ߑ᭄DŽ䖭ḋϡ߽Ѣᠽሩ੠㓈ᡸDŽ 䆹㉏㱑✊ᅠ៤њᇍᏆ᳝ϸϾᇍ䈵ⱘ๲ᔎDŽ } {} BufferedWriter(TextWriter mw) {} BufferedWriter(MediaWriter mw) { class BufferdWriter ऎेৃDŽކᡔᴃ䖯㸠ᦣ䗄ˈᇚ䳔㽕๲ᔎⱘᇍ䈵Ӵ䗦㒭㓧ކ ৃϡৃҹᇍ㓧 ᡔᴃDŽކ ݊ᅲᄤ㉏䛑ᰃ೼Փ⫼㓧 䙷Мᰃ৺ৃҹᇍ݊䖯㸠ϔϟӬ࣪ਸ਼˛ 㱑✊ৃҹᅲ⦄ˈԚᰃ㒻ᡓԧ㋏বⱘᕜ㞗㚓DŽ ⱘᄤ㉏ BufferedDŽ ᔧ Writer Ёᄤ㉏ᇍ䈵䖛໮ˈ䙷МЎњᦤ催↣ϔϾᇍ䈵ᬜ⥛ˈ↣ϔϾᇍ䈵䛑᳝ϔϾ㞾Ꮕ |--BufferedTextWriter |--TextWriter |--BufferedMediaWriter |--MediaWriter Writer˖˄⊼˖ϡ㽕䇃㾷ˈҹϟϸϾᇍ䈵ϡᄬ೼ˈাЎВ՟DŽ˅ 䗮䖛᠔ᄺдⱘ㒻ᡓ⡍ᗻDŽৃҹᓎゟᄤ㉏໡ݭ⠊㉏Ёⱘ write ᮍ⊩DŽेৃ ᡔᴃDŽކ 䳔∖˖ᛇ㽕ᇍ᭄᥂ⱘ᪡԰ᦤ催ᬜ⥛ˈህ⫼ࠄњ㓧 Ā߯ᓎāⱘˈϡ㽕䇃㾷DŽ˅ ˄⊼˖MediaWriter Ϣ TextWtiter ϸϾ㉏೼ JDK Ёᑊϡᄬ೼ˈЎњ᳈ᔶ䈵ⱘВ՟䇈ᯢ㗠 |--TextWriter |--MediaWriter Writer 䗮ᐌ㺙佄㉏੠㹿㺙佄㉏䛑ৠሲϢϔϾ⠊㉏៪㗙᥹ষDŽ ᅗ↨㒻ᡓ᳝᳈དⱘ♉⌏ᗻDŽ 䆹῵ᓣ੠㒻ᡓ᳝ҔМऎ߿ਸ਼˛ ᇍϔ㒘ᇍ䈵䖯㸠ࡳ㛑ⱘ๲ᔎDŽ 䖭гᰃϔ⾡䆒䅵῵ᓣⱘԧ⦄˖㺙㺙佄䆒䅵῵ᓣDŽ ᅗⱘߎ⦄෎Ѣ⌕ᑊ๲ᔎњ⌕ⱘࡳ㛑DŽ 10 JAVA ᬍব ⫳⌏ JAVA ᬍব ⫳⌏ 11 ৠᯊ BufferedWriter Ёϔḋ݋ԧ write ᮍ⊩ˈাϡ䖛ᰃ๲ᔎৢⱘ writeDŽ ᠔ҹ BuferedWriter гᑨ䆹ᰃ Writer ЁⱘϔϾᄤ㉏DŽ class BufferedWriter extends Writer { private Writer w; BufferedWriter(Writer w) { this.w = w; } } Writer |--MediaWriter |--TextWriter |--BufferedWriter 䖭ḋህӮথ⦄㺙佄䆒䅵῵ᓣˈӬ࣪๲ᔎࡳ㛑ⱘ䚼ߚDŽ↨㒻ᡓ㽕♉⌏ᕜ໮DŽ üüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüü ৃҹ೼䇏ϔ㸠ⱘ෎⸔Ϟ⏏ࡴϔϾ㸠োDŽ class MyLineNumberReader extends MyBufferedReader { private int number; MyLineNumberReader(Reader r) { super(r); } public String myReadLine() { number++; return super.myReadLine(); } public void setNumber(int number) { this.number = number; } public int getNumber() { return number; } } ;() bufis.close bufos.close(); } bufos.write(by); { while((by=bufis.read())!=-1) int by = 0; new BufferedOutputStream(new FileOutptStream("2.jpg")); BufferedOutputStream bufos = BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("1.jpg")); 䳔∖˖copy ϔϾ೒⠛DŽ System.out.println(new String(buf)); fis.read(buf); //Ԛᰃ䖭᳝ϔϾᓞッˈህᰃ᭛ӊ䖛໻ˈ໻ᇣ䍙ߎ jvm ⱘݙᆍぎ䯈ᯊˈӮݙᄬ⑶ߎDŽ ऎDŽކ byte[] buf = new byte[fis.available()];//߯ᓎϔϾ߮߮དⱘ㓧 //ৃҹ䖭ḋ᪡԰DŽ //བᵰ᭛ӊԧ⿃ϡᰃᕜ໻DŽ //fis.available();//㦋প݇㘨ⱘ᭛ӊⱘᄫ㡖᭄DŽ FileInputStream fis = new FileInputStream("a.txt"); fos.close();//া݇䯁䌘⑤DŽ fos.write("abcde");//Ⳉ᥹ᇚ᭄᥂ݭܹࠄњⳂⱘഄDŽ FileOutputStream fos = new FileOutputStream("a.txt"); ᄫ㡖⌕Փ⫼ⱘ᭄㒘ᰃᄫ㡖᭄㒘DŽbyte [] bt ⊼ᛣ˖ᄫヺ⌕Փ⫼ⱘ᭄㒘ᰃᄫヺ᭄㒘DŽchar [] chs ᄫ㡖⌕ৃҹ᪡԰ӏԩ᭄᥂DŽ ᢑ䈵෎㉏˖InputStreamˈOutputStreamDŽ ᄫ㡖⌕˖ 12 JAVA ᬍব ⫳⌏ 2ˈOutputStreamWriter˖ᄫヺࠄᄫ㡖ⱘḹṕDŽ 1ˈInputStreamReader˖ᄫ㡖ࠄᄫヺⱘḹṕDŽ ݋ԧⱘᇍ䈵ԧ⦄˖ 2ˈ⌕᪡԰ⱘ᭄᥂䳔㽕䖯㸠㓪ⷕ㸼ⱘᣛᅮᯊDŽ 1ˈᔧᄫ㡖੠ᄫヺП䯈᳝䕀ᤶࡼ԰ᯊDŽ ҔМᯊ׭Փ⫼ਸ਼? 2ˈ䆹⌕ᇍ䈵Ёৃҹᇍ䇏পࠄⱘᄫ㡖᭄᥂䖯㸠ᣛᅮ㓪ⷕ㸼ⱘ㓪ⷕ䕀ᤶDŽ 1ˈᰃᄫ㡖⌕੠ᄫヺ⌕П䯈ⱘḹṕDŽ ⡍⚍˖ 䕀ᤶ⌕˖ ㅔऩ䇈:read ᮍ⊩ᇍ䇏ࠄⱘ᭄᥂䖯㸠ᦤछDŽwrite ᇍ᪡԰ⱘ᭄᥂䖯㸠䕀ᤶDŽ াݭܹ䆹 int ㉏ൟⱘ᭄ؐⱘ᳔ԢϔϾᄫ㡖˄8 ԡ˅DŽ ᇍѢ write ᮍ⊩ˈৃҹϔ⃵ݭܹϔϾᄫ㡖ˈԚ᥹ᬊⱘᰃϔϾ int ㉏ൟ᭄ؐDŽ ݋ԧ᪡԰ᰃ˖byte&255 or byte&0xff ԡ㸹 0. ᠔ҹᇚ䇏ࠄⱘϔϾᄫ㡖㒭ᦤछЎϔϾ int ㉏ൟⱘ᭄ؐˈԚᰃাֱ⬭ॳᄫ㡖ˈᑊ೼࠽ԭѠ䖯ࠊ ℶDŽذᇐ㟈䇏পӮᦤࠡ 㗠䖲㓁䇏ࠄ 8 Ͼ 1ˈህᰃ-1.ˈމ㗠೼᠔᪡԰ⱘ᭄᥂Ёⱘᕜᆍᯧߎ⦄䖲㓁໮Ͼ 1 ⱘᚙ ಴Ў read ᮍ⊩䇏ࠄ᳿ሒᯊ䖨ಲⱘᰃ-1. ᄫ㡖⌕ⱘ read()ᮍ⊩䇏পϔϾᄫ㡖DŽЎҔМ䖨ಲⱘϡᰃ byte ㉏ൟˈ㗠ᰃ int ㉏ൟਸ਼˛ BufferedOutputStream BufferedInputStream FileOutputStream FileInputStream ᄫ㡖⌕˖ BufferedWriter. BufferedReader FileWriter. FileReader. ᄫヺ⌕˖ Ⳃࠡᄺдⱘ⌕ᇍ䈵˖ 13 JAVA ᬍব ⫳⌏ བᵰ㽕Փ⫼ᣛᅮ㓪ⷕ㸼ᯊˈᖙ乏Փ⫼䕀ᤶ⌕DŽ ೼Փ⫼ FileReader ᪡԰᭛ᴀ᭄᥂ᯊˈ䆹ᇍ䈵Փ⫼ⱘᰃ咬䅸ⱘ㓪ⷕ㸼DŽ ⊼ᛣ˖ া㽕㒻ᡓ䆹䕀ᤶ⌕ˈ㦋প݊ᮍ⊩ˈህৃҹⳈ᥹᪡԰᭛ᴀ᭛ӊЁⱘᄫヺ᭄᥂њDŽ ⫼೼䞡ᮄᅮНњˈ 䕀ᤶ⌕Ꮖ㒣ᅠ៤њ㓪ⷕ䕀ᤶⱘࡼ԰ˈᇍѢⳈ᥹᪡԰ⱘ᭛ᴀ᭛ӊⱘ FileReaer 㗠㿔ˈህϡ ᶹⱘᰃ咬䅸ⷕ㸼DŽ䙷М䕀⌕ⱘ read ᮍ⊩ህৃҹ䖨ಲϔϾᄫヺ↨བЁ᭛DŽ ᑊএᶹᣛᅮⱘ㓪ⷕ㸼ˈབᵰ㓪ⷕ㸼≵᳝ᣛᅮˈ ˈټ ೼ᑩሖ䇗⫼ᄫ㡖⌕ⱘ read ᮍ⊩ᯊᇚ㦋পⱘϔϾ៪㗙໮Ͼᄫ㡖᭄᥂䖯㸠Јᯊᄬ 䕀ᤶ⌕Ёⱘ read ᮍ⊩DŽᏆ㒣㵡ܹњ㓪ⷕ㸼ˈ |--FileWriter |--OutputStreamWriter Writer |--FileReader |--InputStreamReader Reader ᪡԰᭛ӊⱘᄫヺ⌕ᇍ䈵ᰃ䕀ᤶ⌕ⱘᄤ㉏DŽ 㸼DŽ OutputStreamWriter(OutputStream,String charSet):䗮䖛䆹ᵘ䗴ߑ᭄߱ྟ࣪ˈৃҹᣛᅮ㓪ⷕ ⷕ㸼 GBKDŽ OutputStreamWriter(OutputStream):䗮䖛䆹ᵘ䗴ߑ᭄߱ྟ࣪ˈՓ⫼ⱘᰃᴀ㋏㒳咬䅸ⱘ㓪 InputStreamReader(InputStream,String charSet):䗮䖛䆹ᵘ䗴ߑ᭄߱ྟ࣪ˈৃҹᣛᅮ㓪ⷕ㸼DŽ GBKDŽ InputStreamReader(InputStream):䗮䖛䆹ᵘ䗴ߑ᭄߱ྟ࣪ˈՓ⫼ⱘᰃᴀ㋏㒳咬䅸ⱘ㓪ⷕ㸼 ᵘ䗴ߑ᭄˖ ᴹDŽ 䙷МᅗӀ᳝䕀ᤶ԰⫼ˈ㗠ᴀ䑿জᰃᄫヺ⌕DŽ᠔ҹ೼ᵘ䗴ⱘᯊ׭ˈ䳔㽕Ӵܹᄫ㡖⌕ᇍ䈵䖯 䖭ϸϾ⌕ᇍ䈵ᰃᄫヺ⌕ԧ㋏Ёⱘ៤ਬDŽ 14 JAVA ᬍব ⫳⌏ .ⱘ䛑ᰃ᭛ᴀ᭄᥂ˈ䙷МৃҹՓ⫼ᄫヺ⌕䕗Ўᮍ֓:Writerټ ᕔ᭛ӊЁᄬ ᮶✊ᰃ᭄᥂∛ˈ䙷Мϔᅮᰃ䕧ߎ⌕ˈৃҹ⫼ⱘ OutputStream,WriterDŽ ᭄᥂∛˖ϔϾ᭛ӊˈ⹀ⲬDŽ BufferedReader bur = new BufferedReader(new InputStreamReader(System.in)); BufferedReader ऎ˖ކ ᥹ϟᴹˈᰃ৺䳔㽕ᦤ催ᬜ⥛ਸ਼˛བᵰ䳔㽕ˈ䙷Мህࡴܹᄫヺ⌕ⱘ㓧 ᠔ҹ㽕Փ⫼ Reader ԧ㋏Ё˖InputStreamReader ᄫヺेৃDŽ থ⦄ System.in ᇍᑨⱘ⌕ᰃᄫ㡖䇏প⌕DŽ᠔ҹ㽕ᇚ݊䖯㸠䕀ᤶˈᇚᄫ㡖䕀៤ ReaderDŽ ಴Ў䬂Ⲭᔩܹ䖯ᴹⱘϔᅮᰃ㒃᭛ᴀ᭄᥂ˈ᠔ҹৃҹՓ⫼ϧ䮼᪡԰ᄫヺ᭄᥂ⱘ ᮶✊ᰃ⑤ˈՓ⫼ⱘህᰃ䕧ܹ⌕ˈৃ⫼ⱘԧ㋏᳝ InputStreamˈReaderDŽ ᭄᥂⑤˖System.in ࠄϔϾ᭛ӊЁDŽټ 1ˈᇚ䬂Ⲭᔩܹⱘ᭄᥂ᄬ 䳔∖˖ ᭄᥂∛˖᥻ࠊৄ System.out,⹀Ⲭ File ᓔ༈ⱘ⌕ᇍ䈵ˈݙᄬ(᭄㒘)DŽ ᭄᥂⑤˖䬂Ⲭ System.in,⹀Ⲭ File ᓔ༈ⱘ⌕ᇍ䈵ˈݙᄬ(᭄㒘)DŽ ݊ᅲᰃЎњᯢ⹂ᄫヺ⌕䖬ᰃᄫ㡖⌕DŽ 2ˈᯢ⹂᪡԰ⱘ᭄᥂ᰃ৺ᰃ㒃᭛ᴀ᭄᥂DŽ ݊ᅲᰃЎњᯢ⹂䕧ܹ⌕䖬ᰃ䕧ߎ⌕DŽ 1ˈᯢ⹂᭄᥂⑤੠᭄᥂∛˄᭄᥂Ⳃⱘ˅DŽ ⌕᪡԰ⱘ෎ᴀ㾘ᕟDŽ üüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüü InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"utf-8"); 䙷М䕀ᤶ⌕ᖙ乏Փ⫼DŽ 䙷М೼䇏পᯊˈህᖙ乏ᣛᅮ㓪ⷕ㸼DŽ བᵰ a.txt Ёⱘ᭛ӊЁⱘᄫヺ᭄᥂ᰃ䗮䖛 utf-8 ⱘᔶᓣ㓪ⷕDŽ 䖭ϸহⱘҷⷕⱘᛣНⳌৠDŽ InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt")); ᪡԰ a.txt Ёⱘ᭄᥂Փ⫼ⱘгᰃᴀ㋏㒳咬䅸ⱘ GBKDŽ FileReader fr = new FileReader("a.txt");//᪡԰ a.txt ⱘЁⱘ᭄᥂Փ⫼ⱘᴀ㋏㒳咬䅸ⱘ GBKDŽ 15 JAVA ᬍব ⫳⌏ 䖭ϝϾ䛑ৃҹ⣀ゟᅠ៤ˈᑊݭᇍ䖛⿟ˈ੠ҷⷕDŽ䙷М IO ⌕ⱘ᪡԰ˈ઺њʽ ৠϞˈ㞾Ꮕϔᅮ㽕ࡼ᠟DŽ 3ˈ໡ࠊ᭛ӊDŽ ৠϞˈ㞾Ꮕϔᅮ㽕ࡼ᠟DŽ 2ˈᇚϔϾ᭛ᴀ᭛ӊⱘ᭄᥂ሩ⼎೼᥻ࠊৄϞDŽ FileOutputStream("a.txt"),charSet); BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new String charSet = "utf-8"; //String charSet = System.getProperty("file.encoding"); FileOutputStreamDŽ 䙷Мህ㽕Փ⫼ OutputStream ԧ㋏Ёৃҹ᪡԰ⱘ᭛ӊⱘᄫ㡖⌕ᇍ䈵˖ ߎࠄϔϾ᭛ӊЁˈ 䕧ߎ䕀ᤶ⌕㽕᥹ᬊϔϾᄫ㡖䕧ߎ⌕䖯ᴹˈ᠔ҹ㽕ᰃ⫼ OutputStram ԧ㋏ˈ㗠᳔㒜䕧 䅸㓪ⷕ㸼DŽ ⊼ᛣ˖㱑✊ᰃ᳔㒜ᰃ᭛ӊˈԚᰃϡৃҹ䗝ᢽ FileWriterDŽ಴Ў䆹ᇍ䈵ᰃՓ⫼咬 ᰃ৺㽕ᦤ催ᬜ⥛ˈᰃˈ䗝ᢽ BufferedWriterDŽ ಴Ў㽕ᣛᅮ㓪ⷕ㸼ˈ᠔ҹ㽕Փ⫼ Writer Ёⱘ䕀ᤶ⌕ˈOutputStreamWriterDŽ ᮶✊ᰃ᭛ᴀ᭄᥂ˈ㗠ᰃ䖬ᰃݭܹࠄ᭛ӊЁˈ䙷МՓ⫼ⱘԧ㋏䖬ᰃ WriterDŽ 䰘ࡴ䳔∖˖Ꮰᳯᇚ䖭ѯ᭛ᴀ᭄᥂ᣝ✻ᣛᅮⱘ㓪ⷕ㸼ᄬܹ᭛ӊЁDŽ BufferedWriter bufr = new BufferedWriter(new FileWriter("a.txt")); ᰃ৺㽕ᦤ催ᬜ⥛ਸ਼˛ᰃˈ䙷ህՓ⫼ BufferedWriter. ಴Ў᪡԰ⱘᰃϔϾ᭛ӊDŽ᠔ҹՓ⫼ Writer Ёⱘ FileWriterDŽ 16 JAVA ᬍব ⫳⌏ 䗮ᐌৃ⫼Ѣ䕃ӊՓ⫼⃵᭄ⱘ䰤ᅮDŽ 㒗д˖䆄ᔩϔϾ⿟ᑣ䖤㸠ⱘ⃵᭄ˈᔧ⒵䎇ᣛᅮ⃵᭄ᯊˈ䆹⿟ᑣህϡৃҹݡ㒻㓁䖤㸠њDŽ ⱘᯊ䯈DŽټᯊˈӮᏺⴔᔧᯊᄬټ Փ⫼䆹ᮍ⊩ᄬ store(Writer ,String comment): jdk1.6 ⠜ᴀDŽᦤկњᮄⱘᮍ⊩DŽ store(OutputStram,String comment)ᮍ⊩DŽ DŽټ 6ˈৃҹᇚ䲚ড়Ёⱘ᭄᥂䖯㸠ᣛᅮⳂⱘⱘᄬ ⊼ᛣ˖⌕Ёⱘ᭄᥂㽕ᰃ "䬂=ؐ" ⱘ㾘᭄߭᥂DŽ load(Reader): jdk1.6 ⠜ᴀDŽᦤկњᮄⱘᮍ⊩DŽ load(InputStream): 5ˈৃҹᇚ⌕Ёⱘ㾘᭄߭᥂ࡴ䕑䖯㸠䲚ড়ˈᑊ⿄Ў䬂ؐᇍDŽ ࠄ prop.txt ᭛ӊЁDŽټ list(new PrintStream("prop.txt")):ᇚ䲚ড়Ёⱘ䬂ؐᇍᄬ ՟˖list(System.out):ᇚ䲚ড়Ёⱘ䬂ؐᇍᠧॄࠄ᥻ࠊৄDŽ list(PrintWriter); list(PrintStream); 4ˈ߫ߎ䆹䲚ড়Ёⱘ᠔᳝䬂ؐᇍˈৃҹ䗮䖛খ᭄ᠧॄ⌕ᣛᅮ߫ߎࠄⱘⳂⱘഄDŽ Set stringPropertyNames(); ೼ jdk1.6 ⠜ᴀ㒭䆹㉏ᦤկϔϾᮄⱘᮍ⊩DŽ Enumeration propertyNames(); ㋴˖ܗ 3ˈ㦋প䲚ড়Ё᠔᳝䬂 2ˈ㦋পᣛᅮ䬂ᇍᑨⱘؐ˖value getProperty(key); 1ˈᄬܹ䬂ؐᇍ˖setProperty(key,value); Properties:䆹䲚ড়ϡ䳔㽕⊯ൟˈ಴Ў䆹䲚ড়Ёⱘ䬂ؐᇍ䛑ᰃ String ㉏ൟDŽ |--Properties |--Hashtable Map ৃҹ੠⌕Ⳍ݇㘨ⱘ䲚ড়ᇍ䈵 Properties. 17 JAVA ᬍব ⫳⌏ ˖ 4ˈ㦋প 䱤㮣᭛ӊDŽ 䘡ग़ܡ ᇍѢ㋏㒳ⳂᔩЁⱘ᭛ӊˈjava ᰃ᮴⊩䆓䯂ⱘˈ᠔ҹ೼䘡ग़ˈৃҹ䙓 ᯊˈ boolean isHidden():߸ᮁ᭛ӊ៪㗙᭛ӊ།ᰃ৺䱤㮣DŽ೼㦋প⹀Ⲭ᭛ӊ៪㗙᭛ӊ། boolean isDirectory():߸ᮁ File ᇍ䈵Ёᇕ㺙ⱘᰃ৺ᰃ᭛ӊ།DŽ boolean isFile(): ߸ᮁ File ᇍ䈵Ёᇕ㺙ⱘᰃ৺ᰃ᭛ӊDŽ boolean exists()˖߸ᮁ᭛ӊ៪㗙᭛ӊ།ᰃ৺ᄬ೼DŽ boolean canRead(); boolean canWrite(): boolean canExecute(): 3ˈ߸ᮁ˖ ᯊᠻ㸠ߴ䰸ࡼ԰DŽ void deleteOnExit(): ߴ䰸ࡼ԰Ѹ㒭㋏㒳ᅠ៤DŽ᮴䆎ᰃ৺ড⫳ᓖᐌˈ㋏㒳೼䗔ߎ ᇍѢᏺ᳝ݙᆍⱘ᭛ӊ།ˈϡৃҹⳈ᥹ߴ䰸ˈᖙ乏㽕Ң䞠ᕔ໪ߴ䰸DŽ ⊼ᛣ˖ᇍѢ᭛ӊ།া㛑ߴ䰸ϡᏺݙᆍⱘぎ᭛ӊ།ˈ boolean delete():ৃ⫼Ѣߴ䰸᭛ӊ៪㗙᭛ӊ།DŽ 2ˈߴ䰸˖ boolean mkdirs(): ߯ᓎ໮㑻᭛ӊ།DŽ boolean mkdir(): ߯ᓎ᭛ӊ།DŽ ᄬ೼ˈ߭ϡ߯ᓎDŽ boolean createNewFile()throws IOException:߯ᓎ᭛ӊˈབᵰ㹿߯ᓎⱘ᭛ӊᏆ㒣 1ˈ߯ᓎ˖ 3ˈᐌ㾕ᮍ⊩˖ ՟ᄤ˖File file = new File("c:"+File.separator+"abc"+File.separator+"a.txt"); 2ˈ⡍߿ⱘᄫ↉˖separator:䎼ᑇৄⱘⳂᔩߚ䱨ヺDŽ File(File parent,String child); File(String parent,String child); ᄬ೼ⱘˈгৃҹᰃϡᄬ೼DŽ File(String filename):ᇚϔϾᄫヺІ䏃ᕘ(Ⳍᇍ៪㗙㒱ᇍ)ᇕ㺙៤ File ᇍ䈵ˈ䆹䏃ᕘᰃৃ 1ˈᵘ䗴ߑ᭄˖ ৃҹ䗮䖛ᇍ䈵ⱘᗱᛇᴹ᪡԰᭛ӊҹঞ᭛ӊ།DŽ 䆹㉏ⱘߎ⦄ᰃᇍ᭛ӊ㋏㒳ⱘЁⱘ᭛ӊҹঞ᭛ӊ།䖯㸠ᇍ䈵ⱘᇕ㺙DŽ File ㉏˖ 18 JAVA ᬍব ⫳⌏ f1.renameTo(f2);//ᇚ c Ⲭϟⱘ a.txt ᭛ӊᬍৡЎ b.txt ᭛ӊDŽ File f2 = new File("c:\\b.txt"); File f1 = new File("c:\\a.txt"); renameTo(File): 5ˈ䞡ੑৡ˖ File[] listFiles():㦋পᣛᅮⳂᔩϟⱘ᭛ӊҹঞ᭛ӊ།ᇍ䈵DŽ ⿄DŽ String[] list(Filenamefilter): ৃҹḍ᥂ᣛᅮⱘ䖛Ⓒ఼ˈ䖛Ⓒৢⱘ᭛ӊঞ᭛ӊ།ৡ String[] list():㦋পᣛᅮⳂᔩϟᔧࠡⱘ᭛ӊҹঞ᭛ӊ།ৡ⿄DŽ static File[] listRoots():㦋পⱘᰃ㹿㋏㒳Ё᳝ᬜⱘⲬヺDŽ long lastModified()˖㦋প᭛ӊ៪㗙᭛ӊ᳔ৢϔ⃵ׂᬍⱘᯊ䯈DŽ long length():㦋প᭛ӊ໻ᇣDŽ ⊼ᛣ˖བᵰᇕ㺙ⱘᰃⳌᇍ䏃ᕘˈ䙷М䖨ಲⱘᰃ null. getParent(): 㦋প File ᇍ䈵ᇕ㺙᭛ӊ៪㗙᭛ӊ།ⱘ⠊ⳂᔩDŽ getAbsolutePath():᮴䆎 File ᇍ䈵Ёᇕ㺙ⱘ䏃ᕘᰃҔМˈ㦋পⱘ䛑ᰃ㒱ᇍ䏃ᕘDŽ getPath():File ᇍ䈵Ёᇕ㺙ⱘ䏃ᕘᰃҔМˈ㦋পⱘህᰃҔМDŽ getName():㦋প᭛ӊ៪㗙᭛ӊ།ⱘৡ⿄DŽ 19 JAVA ᬍব ⫳⌏ { ;() return sb.toString } sb.insert(0,"| "); { for(int x=0; x en) ৃҹᇚᵮВЁⱘ໮Ͼ⌕ড়ᑊ៤ϔϾ⌕DŽ ԰⫼˖ৃҹ⫼Ѣ໮Ͼ᭄᥂ⱘড়ᑊDŽ ⊼ᛣ˖಴Ў Enumeration ᰃ Vector Ё⡍᳝ⱘপߎᮍᓣDŽ㗠 Vector 㹿 ArrayList পҷDŽ ᠔ҹ㽕Փ⫼ ArrayList 䲚ড়ᬜ⥛᳈催ϔѯDŽ䙷Мབԩ㦋প Enumeration ਸ਼˛ ArrayList al = new ArrayList(); for(int x=1; x<4; x++) al.add(new FileInputStream(x+".txt")); Iterator it = al.iterator(); Enumeration en = new Enumeration() { public boolean hasMoreElements() { return it.hasNext(); } public FileInputStream nextElement() { return it.next(); } }; //໮Ͼ⌕ህব៤њϔϾ⌕ˈ䖭ህᰃ᭄᥂⑤DŽ SequenceInputStream sis = new SequenceInputStream(en); //߯ᓎ᭄᥂ⳂⱘDŽ FileOutputStream fos = new FileOutputStream("4.txt"); byte[] buf = new byte[1024*4]; int len = 0; while((len=sis.read(buf))!=-1) { fos.write(buf,0,len); } fos.close(); sis.close(); ⱘᇍ䈵ৃҹ䖯㸠㔥㒰Ӵ䕧DŽৢټᄬ DŽټৃҹ䗮䖛䖭ϸϾ⌕ᇍ䈵Ⳉ᥹᪡԰Ꮖ᳝ᇍ䈵ᑊᇚᇍ䈵䖯㸠ᴀഄᣕЙ࣪ᄬ ObjectOutputStream ObjectInputStream ᇍ䈵ⱘᑣ߫࣪DŽ üüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüü ✊ৢ䗮䖛 Properties ᇍ䈵䖯㸠᭄᥂ⱘࡴ䕑੠㦋পDŽ DŽ䆹䜡㕂ৃҹ䗮䖛䬂=ؐⱘᔶᓣᄬ೼DŽټ ৃҹ䗮䖛䜡㕂᭛ӊ䖯㸠ᄬ ᇍѢߛࡆৢˈড়ᑊᰃ䳔㽕ⱘϔѯ⑤᭛ӊⱘֵᙃDŽ ᛇ㽕ড়ᑊՓ⫼ SequenceInputStream ेৃDŽ //䖭ḋህᰃᇚ 1.mp3 ᭛ӊߛࡆ៤໮Ͼ⹢⠛᭛ӊDŽ fis.close(); } fos.close(); fos.write(buf,0,len); fos = new FileOutputStream((count++)+".part); { while((len=fis.read(buf))!=-1) int count = 1; int len = 0; ऎDŽކ byte[] buf = new byte[1024*1024];//ᰃϔϾ 1MB ⱘ㓧 FileOutputStream fos = null; FileInputStream fis = new FileInputStream("1.mp3"); ϔϾ䇏পᇍᑨ໮њ䕧ߎDŽ //བᵰ㽕ϔϾᇍ᭛ӊ᭄᥂ߛࡆDŽ 25 JAVA ᬍব ⫳⌏ ᭄᥂DŽ String readUTF():ᣝ✻ utf-8 ׂᬍ⠜䇏পᄫヺDŽ⊼ᛣˈᅗা㛑䇏 writeUTF()ݭܹⱘᄫヺ ࠽ϟⱘ᭄᥂㉏ൟϔḋDŽ long readLong(); short readShort(); boolean readBoolean():ϔ⃵䇏পϔϾᄫ㡖DŽ int readInt():ϔ⃵䇏পಯϾᄫ㡖ˈᑊᇚ݊䕀៤ int ؐDŽ ᪡԰෎ᴀ᭄᥂㉏ൟⱘᮍ⊩˖ DataInputStream(InputStream); DataInputStream ᪡԰෎ᴀ᭄᥂㉏ൟⱘ⌕ᇍ䈵DŽ üüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüü 䗮ᐌˈ䖭ϸϾᇍ䈵៤ᇍՓ⫼DŽ ᇍᑨ䴲䴭ᗕгϡᛇ㹿ᑣ߫࣪ⱘ៤ਬ㗠㿔ˈৃҹ䗮䖛 transient ݇䬂ᄫׂ佄DŽ ⊼ᛣ˖ᇍᑨ䴭ᗕⱘ៤ਬব䞣ˈϡӮ㹿ᑣ߫࣪DŽ ᅮНᮍᓣ˖static final long serialVersionUID = 42L; བᵰ㹿ᑣ߫࣪ⱘᇍ䈵䳔㽕㹿ϡৠⱘ㉏⠜ᴀ᠔ݐᆍDŽৃҹ೼㉏Ё㞾ᅮН UIDDŽ 䆹ؐ⫼Ѣ߸ᮁ㹿ᑣ߫࣪ⱘᇍ䈵੠㉏᭛ӊᰃ৺ݐᆍDŽ া㽕ᰃ䖭ѯ៤ਬ≵᳝ব࣪ˈ䙷М䆹ؐ↣⃵䖤ㅫ䛑ϔḋDŽ ᴹⱘϔϾ long ൟⱘؐDŽ ⫼Ѣ㒭㉏ᣛᅮϔϾ UIDDŽ䆹 UID ᰃ䗮䖛㉏Ёⱘৃᑣ߫࣪៤ਬⱘ᭄ᄫㅒৡ䖤ㅫߎ Serializable˖䆹᥹ষ݊ᅲህᰃϔϾ≵᳝ᮍ⊩ⱘᷛ䆄᥹ষDŽ ৺߭Ӯᡯߎ˖NotSerializableException void writeObject(Object)˖㹿ݭܹⱘᇍ䈵ᖙ乏ᅲ⦄ϔϾ᥹ষ:Serializable ObjectOutputStream Object readObject():䆹ᮍ⊩ᡯߎᓖᐌ˖ClassNotFountExceptionDŽ ObjectInputStream ϸϾᇍ䈵ⱘ⡍᳝ᮍ⊩˖ 26 JAVA ᬍব ⫳⌏ ऎDŽކ ԰Ў㓧 ᇍѢ᪡԰᭄㒘ⱘݭܹ⌕ˈ೼ᵘ䗴ߑ᭄ৃҹՓ⫼ぎখ᭄DŽ಴Ўᅗݙ㕂њϔϾৃব䭓ᑺ᭄㒘 ᪡԰᭄㒘ⱘ䇏প⌕೼ᵘ䗴ᯊˈᖙ乏㽕ᯢ⹂ϔϾ᭄᥂⑤DŽ᠔ҹ㽕ӴܹⳌᇍᑨⱘ᭄㒘DŽ ᠔ҹ䖭ѯ೼Փ⫼ⱘᯊ׭ϡ䳔㽕 closeDŽ 㗠Ϩ䖭ѯ⌕ᑊ᳾䇗⫼㋏㒳䌘⑤DŽՓ⫼ⱘህᰃݙᄬЁⱘ᭄㒘DŽ ᇍѢ䖭ѯ⌕ˈ⑤ᰃݙᄬDŽⳂⱘгᰃݙᄬDŽ CharArrayWriter CharArrayReader 2ˈ᪡԰ᄫヺ᭄㒘DŽ writeTo(OutputStream); toString(); toByteArray(); ByteArrayOutputStream ByteArrayInputStream 1ˈ᪡԰ᄫ㡖᭄㒘 ᪡԰᭄㒘ⱘ⌕ᇍ䈵DŽ üüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüü 䗮ᐌ៤ᇍՓ⫼DŽ 䗮ᐌা㽕᪡԰෎ᴀ᭄᥂㉏ൟⱘ᭄᥂DŽህ䳔㽕䗮䖛 DataStram 䖯㸠ࣙ㺙DŽ DŽা㛑䗮䖛 readUTF 䇏পDŽټ writeUTF(String):ᣝ✻ utf-8 ׂᬍ⠜ᇚᄫヺ᭄᥂䖯㸠ᄬ ࠽ϟᰃ᭄᥂㉏ൟггϔḋDŽ writeLong(long); writeShort(short); writeBoolean(boolean); ԡ϶ᓗDŽ ⊼ᛣ੠ write(int)ϡৠDŽwrite(int)াᇚ䆹ᭈ᭄ⱘ᳔ԢϔϾ 8 ԡݭܹDŽ࠽ԭϝϾ 8 writeInt(int)˖ϔ⃵ݭܹಯϾᄫ㡖DŽ ᪡԰෎ᴀ᭄᥂㉏ൟⱘᮍ⊩: DataOutputStream(OutputStream): DataOutputStream 27 JAVA ᬍব ⫳⌏ ????//;( System.out.println(s1 String s1 = new String(b,"iso8859-1"); //㾷ⷕDŽ byte[] b = s.getBytes("GBK"); //㓪ⷕDŽ String s = "Դད"; བᵰ㓪ᇍњˈ㾷䫭њˈ᳝ৃ㛑䖬᳝ᬥʽ བᵰ㓪䫭њˈ≵ᬥʽ ᄫ㡖᭄㒘-->ᄫヺІ ˖ 㾷ⷕDŽ䗮䖛 String ㉏ⱘᵘ䗴ߑ᭄ᅠ៤DŽString(byte[],charset); ᄫヺІ -->ᄫ㡖᭄㒘 ˖㓪ⷕDŽ䗮䖛 getBytes(charset); 㓪ⷕ䕀ᤶ˖ UTF-8˖ᇍ unicode 䖯㸠Ӭ࣪ˈ↣ϔϾᄫ㡖䛑ࡴܹњᷛ䆚༈DŽ ⷕ㸼DŽ䛑⫼ϸϾᄫ㡖㸼⼎ϔϾᄫヺDŽޚ unicode:೑䰙ᷛ GBK˖ㅔԧЁ᭛ⷕ㸼ˈ↨ GB2312 㵡ܹ᳈໮ⱘЁ᭛᭛ӊ੠ヺোDŽ GB2312:ㅔԧЁ᭛ⷕ㸼DŽ ISO8859-1:ᢝϕⷕ㸼DŽՓ⫼ 1 Ͼᄫ㡖ᴹ㸼⼎DŽ ᙃѸᤶⷕDŽՓ⫼ⱘᰃ 1 Ͼᄫ㡖ⱘ 7 ԡᴹ㸼⼎䆹㸼ЁⱘᄫヺDŽֵޚ ASCII:㕢೑ᷛ ᐌ㾕ⷕ㸼˖ System.getProperty("file.encoding"); ϟˈ䛑ᰃᴀᴎ咬䅸ⱘⷕ㸼DŽGBK. 䖭Ͼ㓪ⷕ㸼ᗢМᴹⱘ˛މ 咬䅸ᚙ ೼䕀ᤶ⌕Ёᰃৃҹᣛᅮ㓪ⷕ㸼ⱘDŽ Ԛᰃᠧॄ⌕া᳝䕧ߎDŽ ೼ io Ё⍝ঞࠄ㓪ⷕ䕀ᤶⱘ⌕ᰃ䕀ᤶ⌕੠ᠧॄ⌕DŽ 㓪ⷕ䕀ᤶ˖ üüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüüü StringWriterDŽ StringReaderˈ ㉏Ԑⱘᇍ䈵ৠ⧚˖ 䖭޴Ͼ⌕ⱘߎ⦄݊ᅲህᰃ䗮䖛⌕ⱘ䇏ݭᗱᛇ೼᪡԰᭄㒘DŽ 28 JAVA ᬍব ⫳⌏ JAVA ᬍব ⫳⌏ 29 //ᛇ㽕䖬ॳDŽ /* ᇍ s1 ܜ䖯㸠ϔ⃵㾷ⷕⷕ㸼ⱘ㓪ⷕDŽ㦋পॳᄫ㡖᭄᥂DŽ ✊ৢ೼ᇍॳᄫ㡖᭄᥂䖯㸠ᣛᅮ㓪ⷕ㸼ⱘ㾷ⷕDŽ */ byte[] b1 = s1.getBytes("iso8859-1"); String s2 = new String(b1,"gbk"); System.out.println(s2);//ԴདDŽ 䖭⾡ᚙމ೼ tomcat ᳡ࡵ఼Ӯߎ⦄DŽ ಴Ў tomcat ᳡ࡵ఼咬䅸ᰃ iso8859-1 ⱘ㓪ⷕ㸼DŽ ᠔ҹᅶ᠋ッ䗮䖛⌣㾜఼৥᳡ࡵッ䗮䖛 get ᦤѸᮍᓣᦤѸЁ᭛᭄᥂ᯊˈ ᳡ࡵッ㦋পࠄӮՓ⫼ ISO8859-1 䖯㸠Ё᭛᭄᥂ⱘ㾷ⷕDŽӮߎ⦄хⷕDŽ 䖭ᯊህᖙ乏㽕ᇍ㦋পⱘ᭄᥂䖯㸠 iso8859-1 㓪ⷕDŽ✊ৢ೼ᣝ✻义䴶ᣛᅮⱘ㓪ⷕ㸼䖯㸠㾷 ⷕेৃ 㗠ᇍѢ post ᦤѸˈ䖭⾡ᮍ⊩г䗮⫼DŽԚᰃ post ᳝᳈དⱘ㾷އᮍᓣDŽ request.setCharacterEncoding("utf-8");ेৃDŽ ᠔ҹᓎゟᅶ᠋ッᦤѸՓ⫼ post ᦤѸᮍᓣDŽ

下载文档,方便阅读与编辑

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 10 金币 [ 分享文档获得金币 ] 1 人已下载

下载文档

相关文档