你的charset名字不对吧你看一下charset类试试 US-ASCII

解决方案 »

  1.   

    乱码一般和Unicode编码有关系,现在都用utf-8编码格式。
      

  2.   

    编码转换下public  String decodeUnicode(String theString) {      
       
        char aChar;      
       
         int len = theString.length();      
       
        StringBuffer outBuffer = new StringBuffer(len);      
       
        for (int x = 0; x < len;) {      
       
         aChar = theString.charAt(x++);      
       
         if (aChar == '\\') {      
       
          aChar = theString.charAt(x++);      
       
          if (aChar == 'u') {      
       
           // Read the xxxx      
       
           int value = 0;      
       
           for (int i = 0; i < 4; i++) {      
       
            aChar = theString.charAt(x++);      
       
            switch (aChar) {      
       
            case '0':      
       
            case '1':      
       
            case '2':      
       
            case '3':      
       
           case '4':      
       
            case '5':      
       
             case '6':      
              case '7':      
              case '8':      
              case '9':      
               value = (value << 4) + aChar - '0';      
               break;      
              case 'a':      
              case 'b':      
              case 'c':      
              case 'd':      
              case 'e':      
              case 'f':      
               value = (value << 4) + 10 + aChar - 'a';      
              break;      
              case 'A':      
              case 'B':      
              case 'C':      
              case 'D':      
              case 'E':      
              case 'F':      
               value = (value << 4) + 10 + aChar - 'A';      
               break;      
              default:      
               throw new IllegalArgumentException(      
                 "Malformed   \\uxxxx   encoding.");      
              }      
       
            }      
             outBuffer.append((char) value);      
            } else {      
             if (aChar == 't')      
              aChar = '\t';      
             else if (aChar == 'r')      
              aChar = '\r';      
       
             else if (aChar == 'n')      
       
              aChar = '\n';      
       
             else if (aChar == 'f')      
       
              aChar = '\f';      
       
             outBuffer.append(aChar);      
       
            }      
       
           } else     
       
           outBuffer.append(aChar);      
       
          }      
       
          return outBuffer.toString();      
       
         }    
      

  3.   

    哈哈,第一次发帖,刚才学了一下如何回复。的确长度设为1024的话是可以的,也不会出现中文乱码,因为文件编码方式格式就是Unicode,这时候一次就把文件全部读完了。但是不能算解决问题的办法,万一文件很大呢。
    我的文件内容如下,保存的时候选择的Unicode编码。
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    中文
      

  4.   

    import static java.lang.System.*;
    import java.io.*;
    class Main16
    {
    public static void main(String[] args)throws IOException{
    FileInputStream fis = new FileInputStream("read.txt");
    System.setIn(fis);
    byte[] buf = new byte[12];
    int length = 0;
    while((length=System.in.read(buf))>0){
    System.out.print(new String(buf,0,length));
    }
    }
    }
    跟上面说的问题一样,文件内容如下
    “用FileInputStream读取文件
    第二行”,
    buf足够大的时候没有问题,buf = new byte[10]的时候:buf = new byte[12]的时候,运行如下: