黑马程序员技术交流社区

标题: day21-IO流(ByteArrayStream)问题,求解! [打印本页]

作者: steven152    时间: 2014-8-18 21:33
标题: day21-IO流(ByteArrayStream)问题,求解!
跟着毕老师敲的代码,发现和毕老师的不一样,请史位师兄看下,错在哪里?
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();
   }
}




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