为什么这些字符都不能正常显示
import java.io.*;
public class LX_1 {
public static void main(String[] args)throws Exception{
FileReader wj=new FileReader("D:/java.txt");
int aa;
aa=wj.read();
System.out.println("文件内容为:");
while(aa!=(-1)){
System.out.print((char)aa);
aa=wj.read();
}
wj.close();
}
}

解决方案 »

  1.   

    int改成byte试试
      

  2.   

    read是字符流一次
    一次读两字节呀
    用字节流读字符,输出来全是乱码,应该是????!!!!
      

  3.   

    一次不一定读两个字节,GBK、GB2312、Unicode是一个字符两个字节,但UTF8是变长的不过这对楼主的程序没有影响奇怪的是 FileReader 居然没有指定编码方式构造函数,楼主可以尝试使用 InputStreamReader:InputStream is = null;
    Reader reader = null;try{
        is = new FileInputStream("......");
        reader = new InputStreamReader(is, "UTF16");
        ......
    }catch(Exception ex){
        ex.printStacktrace();
    }finally{
        if(reader != null){
            try{ reader.close(); }catch(Exception ex){}
        }
        if(is != null){
            try{ is.close(); }catch(Exception ex){}
        }
    }
      

  4.   

    /**
     * 按字节读取(单个字节读取方式)
     * @param file
     * @return
     * @throws IOException
     * @throws ContextNotEmptyException
     */
    public String readFileContentBySingleByte(File file) throws IOException, ContextNotEmptyException {
    //声明一个1kb大小的储存空间来存放读到的内容。注意如果内容长度超过该空间最大长度,只会保留最后一次读取的内容
    byte[] tdata = new byte[1024], data = new byte[0];
    FileInputStream in = new FileInputStream(file);
    int index = 0, tempbyte;
    //只要有内容,则一定会进入循环
            while ((tempbyte = in.read()) != -1) {
             /**
              * 这里为了能够在内容超出长度时不造成数据丢失,采用了动态扩容方法来增加长度
              * 
              * 注意:数组的扩容是一件很耗时的工作,特别是在内容很多的情况下,如何合理分配初始化空间大小
              * ,以及扩充的大小需要多多考虑
              * 
              * 由于长度是+1的方式增长,所以不可能根据tempbyte来进行数组扩充
              * 现在的策略是,当index与当前长度一样时,以当前长度翻倍来扩充
              */
             if(tdata.length == index){
             tdata = Arrays.copyOf(tdata, 2 * tdata.length);
             }
             tdata[index] = (byte) tempbyte;
             index++;
            }
            in.close();
            if(index == 0) {
             throw new ContextNotEmptyException("文件中没有要读取的内容");
            }
            //截取有效长度的数据
            data = Arrays.copyOf(data, index);
            System.arraycopy(tdata, 0, data, 0, index);
            return new String(data);
    }