import java.io.*;public class FileIn {
public static void main(String[] args) {
try {
FileInputStream rf = new FileInputStream(
"F:\\100个让心情变好的佛家领悟-佛家感悟.txt");
int b;
while ((b = rf.read()) != -1)
System.out.print((char) b);
rf.close();
} catch (IOException ie) {
System.out.println(ie);
} catch (Exception e) {
System.out.println(e);
}
}
}

解决方案 »

  1.   

    是你取的方法不对,而不是中文乱码,你这里面每次去到的只是一个字节,而UTF8编码的中文是2个字节,肯定无法正常输出中文
    import java.io.*;public class FileIn {
        public static void main(String[] args) {
            try {
                FileInputStream rf = new FileInputStream(
                        "F:\\100个让心情变好的佛家领悟-佛家感悟.txt");
                BufferedReader br=new BufferedReader(new InputStreamReader(rf));
    //            int b;
    //            while ((b = rf.read()) != -1)
    //                System.out.print((char) b);
    //            rf.close();
                String s="";
                while((s=br.readLine())!=null){
                     System.out.println(s);
                }
                br.close();
            } catch (IOException ie) {
                System.out.println(ie);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
      

  2.   

    如果你的txt文件是gbk编码的,尝试下面代码读取import java.io.*;public class FileIn {
        public static void main(String[] args) {
            try {
                FileInputStream rf = new FileInputStream(
                        "F:\\100个让心情变好的佛家领悟-佛家感悟.txt");
                BufferedReader br=new BufferedReader(new InputStreamReader(rf,"gbk"));
    //            int b;
    //            while ((b = rf.read()) != -1)
    //                System.out.print((char) b);
    //            rf.close();
                String s="";
                while((s=br.readLine())!=null){
                     System.out.println(s);
                }
                br.close();
            } catch (IOException ie) {
                System.out.println(ie);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
      

  3.   

    UTF-8编码里 中文是3字节。
      

  4.   

     int b;
     while ((b = rf.read()) != -1)
     System.out.print((char) b);
     rf.close();
     } 就算支撑中文,你这个能否打印出正确的字符呢?这个-1只是一个标志位而已,不是你理解的所谓数值,所以你这个用byte转换能得到什么?
    UTF-8编码里,中文是3个字节,能否给个介绍类的文章?谢谢
      

  5.   

    Charset.forName("UTF-8").encode("中").array().length == 3 
    Charset.forName("GBK").encode("中").array().length == 2
      

  6.   

    感谢提醒,记错了,gbk的中文是2字节,utf8是3字节
      

  7.   

    读取中文应该用FileReader字符流处理,不应该用字节流:
    public class FileIn {
    public static void main(String[] args) {
    try {
    FileReader fr = new FileReader(new BufferedReader(
    "F:\\100个让心情变好的佛家领悟-佛家感悟.txt"));
    String b;
    while ((b = fr.readLine()) != null)
    System.out.printb);
    fr.close();
    } catch (IOException ie) {
    System.out.println(ie);
    } catch (Exception e) {
    System.out.println(e);
    }
    }
    }
      

  8.   

    明确的说,不应该用字节流是 浅薄的说法,至少不确切FileReader只能用本地操作系统的编码,无法进行转码读取比如你本地gbk的系统,就无法读取utf8编码的文件这时候需要用InputStreamReader来做
      

  9.   

    [Quote=引用 11 楼 ioe_gaoyong 的回复:]
    明确的说,不应该用字节流是 浅薄的说法,至少不确切FileReader只能用本地操作系统的编码,无法进行转码读取比如你本地gbk的系统,就无法读取utf8编码的文件这时候需要用InputStreamReader来做
    大哥,你果然牛逼,还请以后多多指教啊