这个文件看起来都是整数,实际上是以字符形式存放的
如果文件内容不多,可以一次性读出来,用空格split
readInt方法适合于DataOutput中writeInt写进去的数据
API:This method is suitable for reading bytes written by the writeInt method of interface DataOutput.
程序代码:
public class IOTest {
public static void main(String[] args) throws IOException{
int length = 0;
OutputStream out = new FileOutputStream("d:\\test.txt");
DataOutput output = new DataOutputStream(out);
for(; length < 20; length++){
output.writeInt(length);
}
out.close();
InputStream in = new FileInputStream("d:\\test.txt");
DataInput input = new DataInputStream(in);
while(length > 0){
System.out.println(input.readInt());
length --;
}
in.close();
}
}