日期:2014-05-20  浏览次数:20599 次

IO读取文件问题
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
新手求助

------解决方案--------------------
if ((b=in.read())!=-1)
==>
while ((b=in.read())!=-1)

------解决方案--------------------
逻辑错误了
Java code
try
{
long num = 0;
if ((b=in.read())!=-1)
{
System.out.print((char)b);
num ++;
}

------解决方案--------------------
探讨
if ((b=in.read())!=-1)
==>
while ((b=in.read())!=-1)

------解决方案--------------------
lz 别忘了循环啊
------解决方案--------------------
楼主把if改成while就行啦,if只执行一次。