A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© steven152 中级黑马   /  2014-8-18 21:33  /  747 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

跟着毕老师敲的代码,发现和毕老师的不一样,请史位师兄看下,错在哪里?
import java.io.*;

class RandomAccessFileDemo
{
    public static void main(String [] args) throws IOException
   {
        //writeFile();
        readFile();
   }
    public static void readFile() throws IOException
    {

            RandomAccessFile raf = new RandomAccessFile("ran.txt","r");//只读
            //raf.write("haha".getBytes());       
              raf.seek(8);//加了这行代码,调整指针后,发生报错异常,什么原因?该怎么调整?如果不加这行代码,age显示的却不是年龄,也不知道什么原因?
              byte[] buf = new byte[4];
              raf.read(buf);
              String name = new String(buf);
              //System.out.println("name"+name);
              int age =raf.readInt();          
              System.out.println("name:"+name);
              System.out.println("age:"+age);
            raf.close();       
    }
   public static void writeFile()throws IOException
   {
        RandomAccessFile raf = new RandomAccessFile

("ran.txt","rw");
        raf.write("李四".getBytes());
        raf.write(97);
        raf.write("王五".getBytes());
        raf.writeInt(99);
        raf.close();
   }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马