- package com.RandomAccessFile;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- /*
- * 随机读写访问流
- */
- public class RandomAccessFileDemo {
- public static void main(String arg[]){
- //writes();
- readers();
- }
- public static void readers(){
- try {
- RandomAccessFile raf=new RandomAccessFile("E:\\学习记录\\haha.txt", "r");
- raf.seek(0);
- byte[] buf=new byte[8];
- raf.read(buf);
- String name=new String(buf);
- int age=raf.readInt();
- System.out.println("name="+name+",age="+age);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
-
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void writes(){
- RandomAccessFile raf=null;
- try {
- raf =new RandomAccessFile("E:\\学习记录\\haha.txt", "rw");
- raf.seek(0);
- raf.write("zhaoliu".getBytes());
- raf.writeInt(97);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- if(raf!=null){
- try {
- raf.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
复制代码 读取的时候出现
java.io.EOFException
at java.io.RandomAccessFile.readInt(RandomAccessFile.java:725)
at com.RandomAccessFile.RandomAccessFileDemo.readers(RandomAccessFileDemo.java:22)
at com.RandomAccessFile.RandomAccessFileDemo.main(RandomAccessFileDemo.java:13)
这是怎么回事呢?
|