io流之inputstreamoutputstreamreaderwriter
Posted 我俩绝配
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了io流之inputstreamoutputstreamreaderwriter相关的知识,希望对你有一定的参考价值。
例子程序:读取工作空间下
package io; import java.io.*; public class TestFileInputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; try { in = new FileInputStream("F:\\\\share\\\\workspace\\\\J2SE\\\\src\\\\io\\\\TestFileInputStream.java"); } catch (FileNotFoundException e) { System.out.println("找不到文件"); System.exit(-1); } try { long num = 0; while((b=in.read())!=-1){ System.out.print((char)b); num++; } in.close(); System.out.println(); System.out.println("共读取了 "+num+" 个字节"); } catch (IOException e1) { System.out.println("文件读取错误"); System.exit(-1); } } }
结果:由于read方法一次读取的是一个字节,一个汉字占的2个字节,所以打印出来汉字是乱码,
package io; import java.io.*; public class TestFileInputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; try { in = new FileInputStream("F:\\\\share\\\\workspace\\\\J2SE\\\\src\\\\io\\\\TestFileInputStream.java"); } catch (FileNotFoundException e) { System.out.println("æ¾ä¸å°æ件"); System.exit(-1); } try { long num = 0; while((b=in.read())!=-1){ System.out.print((char)b); num++; } in.close(); System.out.println(); System.out.println("å ±è¯»åäº "+num+" 个åè"); } catch (IOException e1) { System.out.println("æ件读åé误"); System.exit(-1); } } } 共读取了 737 个字节
OutputStream:
例子程序:文件的复制,如果文件不存在会自动创建HW.java 文件
package io; import java.io.*; public class TestFileOutputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("f:/share/j2se/IO/HelloWorld.java"); out = new FileOutputStream("f:/share/j2se/IO/HW.java"); while((b=in.read())!=-1){ out.write(b); } in.close(); out.close(); } catch (FileNotFoundException e2) { System.out.println("找不到指定文件"); System.exit(-1); } catch (IOException e1) { System.out.println("文件复制错误"); System.exit(-1); } System.out.println("文件已复制"); } }
Reader:
例子程序:读取本身
package io; import java.io.*; public class TestFileReader { public static void main(String[] args) { FileReader fr = null; int c = 0; try { fr = new FileReader("F:\\\\share\\\\workspace\\\\J2SE\\\\src\\\\io\\\\TestFileReader.java"); int ln = 0; while ((c = fr.read()) != -1) { //char ch = (char) fr.read(); System.out.print((char)c); //if (++ln >= 100) { System.out.println(); ln = 0;} } fr.close(); } catch (FileNotFoundException e) { System.out.println("找不到指定文件"); } catch (IOException e) { System.out.println("文件读取错误"); } } }
Writer:
例子程序“
package io; import java.io.*; public class TestFileWriter { public static void main(String[] args) { FileWriter fw = null; try { fw = new FileWriter("f:\\\\share\\\\j2se\\\\IO\\\\unicode.dat"); //char在计算机是用数字表示的,直接把数字写进文件。char是2个字节,最大是65535 //这里就是吧Unicode大多数国家的字符都写进了文件 for(int c=0;c<=50000;c++){ fw.write(c); if(c%100==0){ fw.write("\\n"); } } fw.close(); } catch (IOException e1) { e1.printStackTrace(); System.out.println("文件写入错误"); System.exit(-1); } } }
结果:
!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉ ĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲų ŴŵŶŷŸŹźŻżŽžſƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏƐƑƒƓƔƕƖƗƘƙƚƛƜƝƞƟƠơƢƣƤƥƦƧƨƩƪƫƬƭƮƯưƱƲƳƴƵƶƷƸƹƺƻƼƽƾƿǀǁǂǃDŽDždžLJLjljNJNjnjǍǎǏǐǑǒǓǔǕǖǗǘǙǚǛǜǝǞǟ
䓋䓌䓍䓎䓏䓐䓑䓒䓓䓔䓕䓖䓗䓘䓙䓚䓛䓜䓝䓞䓟䓠䓡䓢䓣䓤䓥䓦䓧䓨䓩䓪䓫䓬䓭䓮䓯䓰䓱䓲䓳䓴䓵䓶䓷䓸䓹䓺䓻䓼䓽䓾䓿䔀䔁䔂䔃䔄䔅䔆䔇䔈䔉䔊䔋䔌䔍䔎
䔏䔐䔑䔒䔓䔔䔕䔖䔗䔘䔙䔚䔛䔜䔝䔞䔟䔠䔡䔢䔣䔤䔥䔦䔧䔨䔩䔪䔫䔬䔭䔮䔯䔰䔱䔲䔳䔴䔵䔶䔷䔸䔹䔺䔻䔼䔽䔾䔿䕀䕁䕂䕃䕄䕅䕆䕇䕈䕉䕊䕋䕌䕍䕎䕏䕐䕑
䕒䕓䕔䕕䕖䕗䕘䕙䕚䕛䕜䕝䕞䕟䕠䕡䕢䕣䕤䕥䕦䕧䕨䕩䕪䕫䕬䕭䕮䕯䕰䕱䕲䕳䕴䕵䕶䕷䕸䕹䕺䕻䕼䕽䕾䕿䖀䖁䖂䖃䖄䖅䖆䖇䖈䖉䖊䖋䖌䖍䖎䖏䖐䖑䖒䖓䖔䖕
䖖䖗䖘䖙䖚䖛䖜䖝䖞䖟䖠䖡䖢䖣䖤䖥䖦䖧䖨䖩䖪䖫䖬䖭䖮䖯䖰䖱䖲䖳䖴䖵䖶䖷䖸䖹䖺䖻䖼䖽䖾䖿䗀䗁䗂䗃䗄䗅䗆䗇䗈䗉䗊䗋䗌䗍䗎䗏䗐䗑䗒䗓䗔䗕䗖䗗䗘
䗙䗚䗛䗜䗝䗞䗟䗠䗡䗢䗣䗤䗥䗦䗧䗨䗩䗪䗫䗬䗭䗮䗯䗰䗱䗲䗳䗴䗵䗶䗷䗸䗹䗺䗻䗼䗽䗾䗿䘀䘁䘂䘃䘄䘅䘆䘇䘈䘉䘊䘋䘌䘍䘎䘏䘐䘑䘒䘓䘔䘕䘖䘗䘘䘙䘚䘛䘜
以上是关于io流之inputstreamoutputstreamreaderwriter的主要内容,如果未能解决你的问题,请参考以下文章
io流之转换流InputStreamReaderOutputStreamWriter