我想做个读取txt的文件程序,可在读取不同的编码格式txt文件时候,得到的字符串用println打印出来的时候老有乱码除了默认的ASNI这个编码不出现其他的取了以后老出现乱码?希望大家帮忙解决下

解决方案 »

  1.   

    修改方案为读取xml文件,在xml头定义编码.
    或者在所有txt文件第1行默认放编码格式.然后按指定编码去处理.
      

  2.   

    从底层的角度来说
    都是0101的序列而已
    你读出来的无非都是字符串而已全部读取出来然后再按照给定的格式编码就好了或者全部使用UTF-8嘛
      

  3.   

    File f=new File("D:\\unicode.txt");
    InputStream in=null;
    try {
    File file = new File("D:\\test");
    if (file.isDirectory()) {
    for (File i : file.listFiles()) {
    in = new FileInputStream(i);
    byte[] head = new byte[3];
    in.read(head);
    System.out.println(i.getName());
    if(head[0]==-1&&head[1]==-2){
    System.out.println("this file encoding unicode");
    //处理文件,也可以以行来读取,注意编码就行了。其他编码格式类似。
    InputStream input=new FileInputStream(i);
    byte[]content=new byte[input.available()];
            input.read(content);
            System.out.println(new String(content,"unicode"));
            input.close();
    }else if(head[0]==-2&&head[1]==-1){
    System.out.println("this file encoding unicode big endian");
    }else if(head[0]==-17&&head[1]==-69&&head[2]==-65){
    System.out.println("this file encoding unicode utf-8");
    }else{
    System.out.println("this file encoding ansi");
    }
    }
    }
    in.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }