IO流,使用指定的码表读写字符。

public class Demo_TransIO{

  public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException,IOException{

    InputStreamReader isr = new InputStreamReader (new FileInputStream("utf-8.txt"),"utf-8");    //指定码表读字符

    OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream(GBK.txt"),"GBK");  //指定码表写字符

    int c;

    while(c = isr.read() != -1){

      osw.write(c);

    }

    isr.close();

    osw.close();

  }

}

比上面方法更高效的:

  public static void demo02() throws UnsupportedEncodingException, FileNotFoundException,IOException{

    BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream("utf-8.txt"),"utf-8"));    //指定码表更高效的读字符

    BufferedWriter bw= new BufferedWriter (new OutputStreamWriter(new FileOutputStream(GBK.txt"),"GBK"));  //指定码表更高效的写字符

    int c;

    while(c = br .read() != -1){

      bw.write(c);

    }

    br .close();

    bw.close();

  }

 

猜你喜欢

转载自www.cnblogs.com/wangffeng293/p/13199078.html