现在有个html文件的编码是gb2312;我用java的io想把它读出来写进另外一个文件,不过不管用reader和writer,还是input和output,写出来的都是乱码,java的io里好像也没有可以设置字符编码的东西啊,
求解释:求解决:

解决方案 »

  1.   

    InputStreamReader(InputStream in, Charset cs) 
              创建使用给定字符集的 InputStreamReader。
    OutputStreamWriter(OutputStream out, Charset cs) 
              创建使用给定字符集的 OutputStreamWriter。
      

  2.   

    读取的时候加上编码,用bufferedReader,一行一行的读取
      

  3.   

    package com.daipeng;import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;public class JSMoveTest {
    public static void main(String[] args){
    BufferedReader in = null;
    BufferedWriter out=null;
    File fileD1=null;
    // File fileD2=null;
    // File fileD3=null;
    String file_Location ="C:\\Documents and Settings\\Administrator\\桌面\\html学习\\教程练习\\";
    String fileD1_Location=file_Location+"JS";
    //String fileInformation = null;
    //String fileD2_Name=null;
    // String fileD3_Name=null;
    String Information=null;



    try{ 
    out = new BufferedWriter(new OutputStreamWriter(
    new FileOutputStream(file_Location+"js页面编写.txt"),"utf-8"));
    out.write("*************************************************************************************");
    out.write("文件开始记录");
    out.write("*************************************************************************************");
    out.flush();
    fileD1 = new File(fileD1_Location);
    for(String ss:fileD1.list()){
    for(String s:(new File(fileD1_Location+"\\"+ss)).list()){
    if(s.endsWith(".htm")){
    in = new BufferedReader( new InputStreamReader(new FileInputStream(fileD1_Location+"\\"+ss+"\\"+s),"utf-8"));
    out.write("********************************************");
    out.newLine();
    out.write("-----------"+ss+s+"--"+"start"+"------------------");
    out.newLine();
    out.write("********************************************");
    out.newLine();
    while( (Information =in.readLine())!=null){
    //Information  =Information.getBytes("gb2312").toString();
    // String Information=in.readLine();
    out.write(Information);
    out.newLine();
    }
    out.write("********************************************");
    out.newLine();
    out.write("-----------"+s+"--"+"end"+"------------------");
    out.newLine();
    out.write("********************************************");
    out.newLine();
    out.flush();
    }

    }
    }

    }catch(Exception e){
    e.printStackTrace();
    System.out.println("error"+" happened Exception ");

    }

    }
    }
    这是源码,摆脱高手给解释:
      

  4.   

    你这个读写的时候都是使用utf-8的吧
      

  5.   

    不是的,html的文件时用gb2312编码的,可是我试过gb2312不行,才改成utf-8的,结果还是不行。
      

  6.   

    没有文件没法测试,要不你把文件发给我看看?[email protected]
      

  7.   

    我看你htm文件编码格式是unicode,把in = new BufferedReader( new InputStreamReader(new FileInputStream(fileD1_Location+"\\"+ss+"\\"+s),"utf-8"));改为in = new BufferedReader( new InputStreamReader(new FileInputStream(fileD1_Location+"\\"+ss+"\\"+s),"utf-16"));试试
      

  8.   

    高人啊,解决了,不过我还有个问题,html文件里不是
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    吗,还是文件中用到的也有中文啊,为什么时utf-16的编码啊,怎么看出来的啊?
    大虾!!
      

  9.   

    其实我也是新手,我是用记事本打开,然后选另存为,看到当前是unicode。
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>是指定网页显示时候使用的编码,即使写的不太对,很多浏览器也能自适应。