import java.io.*;
public class IODemo2
{
   public static void main(String[] args)
   {
int i;
char b;
try
{
            FileReader  in = new FileReader  ("c:\\jdk\\source.txt"));
    i=0;
    while(i>=0)
    {
try
{
b=in.read();
System.out.print(b);
i++;
}
catch(Exception e)
{
    System.out.println();
    System.out.println("总数为 "+i);
    in.close();
    i=-1;
                }
    }
}
catch(Exception e){}
    }
}

解决方案 »

  1.   

    To:ggyy
      你的程序不可行。
      b声明为char,但赋值时却出现了b=in.read(),read()返回的是int型,程序报错。
      我再说明一下:source.txt中存的是中文  
      
      

  2.   

    用:
    String strResult="";
    .
    .
    .
    String str=in.readLine();
    strResult=strResult+str;.
    .
    .
    System.out.println("总数为 "+strResult.Length);
      

  3.   

    import java.io.*;
    public class IODemo2
    {
       public static void main(String[] args)
       {
    int i;

    try
    {
                FileReader in = new FileReader("C:\\jdk\\source.txt");
        i=0;
        while(i>=0)
        {
    try
    {
    i=in.read();
    System.out.print((char)i);

    }
    catch(Exception e)
    {
       
                    }
        }
                  in.close();
    }
    catch(Exception e){
            System.out.println(e.getMessage());
          }
        }
    }
      

  4.   

    建议使用StringBuffer代替String,不然文件大了会很慢的,最好是一行统计一次,最后相加
      

  5.   

    to:
    ggyy(没钱?滚蛋!--沧海桑田,伶仃过洋) 你这样不可以吧?
    读入中文的时候你会把一个字拆成两个字吧?
      

  6.   

    ??
    char是16位
    汉字编码也是16位(unicode)
    ???
      

  7.   

    read
    public int read()
             throws IOExceptionRead a single character.
    Overrides:
    read in class Reader
    Returns:
    The character read, or -1 if the end of the stream has been reached
    Throws:
    IOException - If an I/O error occurs
    ///所以read是读取一个char的,只不过又将他转化为了32位了。
    //但是不是说读取32位的(2个字节)
    System.out.print((char)i);
    所以不会读入中文的时候你会把一个字拆成两个字