- import java.io.*;
- class RandomAccessFileDemo
- {
- public static void main(String[] args) throws IOException
- {
- //writerFile();
- readerFile();
- }
- public static void readerFile()throws IOException
- {
- RandomAccessFile raf = new RandomAccessFile("file.txt","r");
-
- byte[] arr = new byte[4];
- raf.read(arr);
-
-
- String name = new String(arr);
-
- int age = raf.readInt();
-
- System.out.println(name);
- System.out.println(age);
- byte[] arr1 = new byte[4];
- raf.read(arr1);
-
-
- String name1 = new String(arr1);
-
- int age1 = raf.readInt();
-
- System.out.println(name1);
- System.out.println(age1);
- raf.close();
-
- }
- public static void writerFile()throws IOException
- {
- RandomAccessFile raf = new RandomAccessFile("file.txt","rw");
- raf.write("李四".getBytes());
- raf.writeInt(97);
- raf.write("李".getBytes());
- raf.writeInt(99);
- raf.close();
-
-
- }
- }
复制代码
大侠给看一下 为什么会有 异常抛出的啊 |
|