本帖最后由 贾存双 于 2012-7-17 07:39 编辑
import java.io.* ;
public class Demo{
public static void main(String args[])throws Exception{
File f = new File("e:" + File.separator + "io" + File.separator + "RandomAccessFile" + File.separator + "Random.txt") ;
RandomAccessFile raf = new RandomAccessFile(f,"rw") ;
String name = null ;
int age = 0 ;
name = "Jrain " ;
age = 22 ;
raf.writeBytes(name) ;
raf.writeInt(age) ;
name = "xiaojian" ;
age = 20 ;
raf.writeBytes(name) ;
raf.writeInt(age) ;
raf.close() ;
RandomAccessFile rf = new RandomAccessFile(f,"r") ;
byte b[] = new byte[8] ;
rf.skipBytes(12) ;
for(int i = 0;i<b.length;i++){
b = rf.readByte() ;
}
/*for(byte i:b){
i = rf.readByte() ;
}*/ 如果把for循环改为foreach的形式,则byte数组读不到信息,是怎么回事???
int a = rf.readInt() ;
String str = new String(b) ;
System.out.println("读取到的姓名:" + str + "; 年龄:" + a) ;
}
}
这是普通的for循环打印出来的结果:
这是用foreach打印出来的结果:
|