java中输入流中read()方法的使用
import java.io.*;
public class InputExa
{
public static void main(String[] args)
{ int b;
byte tom[]=new byte[10];
try
{
File f=new File("G://java code/jackxu/src","WaitExa.java");
FileInputStream in=new FileInputStream(f);
b=in.read(tom);
String s=new String(tom);
b=in.read(tom,0,5);
s=new String(tom);
System.out.println("第一次输出"+s);
}
catch(IOException e)
{ System.out.println("File Read Error"+e);
}
in.close();
}
}
上面的程序是读取WaitExa.java文件,把读取的字节转化为字符串输出,b=in.read(tom)这是第一个read()方法,它读取的字节数没确定,就由定义的字节数组确定。b=in.read(tom,0,5);这是第二次使用read()方法,这次规定了字节读取的顺序和长度,结果输出来的还是按第一次byte[10]中输出了10无序字符,这是怎么回事??