黑马程序员技术交流社区

标题: 求大神来指点错在哪了 [打印本页]

作者: 走遍世界找寻你    时间: 2013-8-20 18:39
标题: 求大神来指点错在哪了
本帖最后由 走遍世界找寻你 于 2013-8-21 08:54 编辑

import java.io.*;
class  RandomAccessFileDemo
{
        public static void main(String[] args) throws IOException
        {
                writeFile();
        }
        public static void readFile()throws IOException
        {
                RandomAccessFile raf = new RandomAccessFile("ran.txt","r");
                //调整指针位置
                raf.seek(8);
                //raf.write("haha".getBytes());
                byte[] nuf = new byte[4];
                raf.read(buf);
                String name = new String(buf);
                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.writeInt(97);
                raf.write("王五".getBytes());
                raf.writeInt(99);

                raf.close();
        }
}



作者: 许云龙    时间: 2013-8-20 18:44
  1. import java.io.*;
  2. class  ClassIs13
  3. {
  4.         public static void main(String[] args) throws IOException
  5.         {
  6.                 readFile();
  7.         }
  8.         public static void readFile()throws IOException
  9.         {
  10.                 RandomAccessFile raf = new RandomAccessFile("ran.txt","r");
  11.                 //调整指针位置
  12.                 raf.seek(8);
  13.                 //raf.write("haha".getBytes());
  14.                 byte[] buf = new byte[4];  //这个地方你定义错了 不是nuf--------------------------------------------
  15.                 raf.read(buf);
  16.                 String name = new String(buf);
  17.                 int age = raf.readInt();
  18.                 System.out.println("name="+name);
  19.                 System.out.println("age="+age);
  20.                 raf.close();
  21.         }
  22.         public static void writeFile()throws IOException
  23.         {
  24.                 RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
  25.                 raf.write("李四".getBytes());
  26.                 raf.writeInt(97);
  27.                 raf.write("王五".getBytes());
  28.                 raf.writeInt(99);

  29.                 raf.close();
  30.         }
  31. }
复制代码

作者: 杨增坤    时间: 2013-8-21 07:38
问题解决了,请把问题更改为已解决!谢谢合作!
作者: 走遍世界找寻你    时间: 2013-8-21 08:45
谢谢大家 太粗心了




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