本帖最后由 寇弘禄 于 2013-4-6 19:27 编辑
- package time20120406;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- public class RandomAccessDemo {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- WriteMethod();
- ReadMethod();
- }
- public static void WriteMethod() throws IOException{
- RandomAccessFile ra = new RandomAccessFile("d:\\ran.txt","rw");
- ra.write("王五".getBytes());
- ra.writeInt(22);
- ra.close();
- }
- public static void ReadMethod() throws IOException{
- RandomAccessFile ra = new RandomAccessFile("d:\\ran.txt","r");
- byte[] buf = new byte[1024];
- int len = ra.read(buf);
- String name = new String(buf,0,len);
- int age = ra.readInt();
- System.out.println(name);
- System.out.println(age);
- ra.close();
- }
- }
复制代码 报的是Exception in thread "main" java.io.EOFException 这个错误。看着和视频里没太大区别啊?? |