黑马程序员技术交流社区

标题: RandomAccess类,错在哪里了? [打印本页]

作者: 一诺千金    时间: 2014-4-1 14:56
标题: RandomAccess类,错在哪里了?
class RandomAccessFileDemo{
    public static voidmain(String[] args) throws IOException{
        write();
        read();
        randomWrite();
    }
   
    public static voidrandomWrite()throws IOException{
        RandomAccessFileraf = new RandomAccessFile("random.txt","r");
        raf.seek(8*4);
        System.out.println("pos:"+raf.getFilePointer());
        raf.write("王武".getBytes());
        raf.writeInt(102);
        raf.close();
    }
    public static voidread()throws IOException{
        RandomAccessFileraf = new RandomAccessFile("random.txt","rw");
     
        raf.seek(8*1);
        System.out.println("pos1:"+raf.getFilePointer());
        byte[] buf = newbyte[4];
        raf.read(buf);
        String name = newString(buf);
        int age =raf.readInt();
        System.out.println(name+"::"+age);
        System.out.println("pos2:"+raf.getFilePointer());
        raf.close();
    }
    public static voidwrite()throws IOException{
      
        RandomAccessFileraf = new RandomAccessFile("random.txt","rw");
      
        raf.write("张三".getBytes());
        raf.writeInt(97);
        raf.close();
    }
}
运行完全没有效果?


作者: osully    时间: 2014-4-1 15:27
把指针偏移 删了就o了
raf.seek(8 * 1);

你干嘛偏移





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2