首页 > 代码库 > io流之inputstream、outputstream、reader、writer

io流之inputstream、outputstream、reader、writer

 

技术分享

技术分享

例子程序:读取工作空间下

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{|}~???????????????????????????????? ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿāā????????????????ēē??????ěě??????????????īī????????????????????????ń???ň???ōō????Œœ????????????Šš????????ūū????????????Ÿ?????????????????????????ƒ??????????????????????????????????????????????∥???????????ǎǎǐǐǒǒǔǔǖǖǘǘǚǚǜǜ???

????????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????????????????????

 

 

io流之inputstream、outputstream、reader、writer