import java.io.*;
public class TestFileInputStream
{
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
try
{
in = new FileInputStream ("d:\\java\\TestFileInputStream.java");
}
catch (FileNotFoundException e)
{
System.out.println ("文件没有找到");
System.exit (-1);
}
try
{
long num = 0;
if ((b=in.read())!=-1)
{
System.out.print((char)b);
num ++;
}
in.close ();
System.out.println ();
System.out.println ("总共字节数" + num);
}
catch (IOException e1)
{
System.out.println ("文件读写错误");
System.exit (-1);
}
}
}为什么运行后,结果显示只能读取字母i
新手求助

解决方案 »

  1.   

    if ((b=in.read())!=-1)
    ==>
    while ((b=in.read())!=-1)
      

  2.   

    逻辑错误了
    try
    {
    long num = 0;
    if ((b=in.read())!=-1)
    {
    System.out.print((char)b);
    num ++;
    }
    应该吧里面的if 改成 for
    如果读取的字节流不是-1(还没到末尾)
    那么就打印出那个字节,然后计数器上增加1
      

  3.   

    +1,直接通过fileStream.available();或者long l = reader.skip(Long.MAX_VALUE)也可以获得总字节数。
      

  4.   

    楼主把if改成while就行啦,if只执行一次。