题目和我写的java代码如下:/*编写一个Java应用程序,利用RandomAccessFile类,把几个int型整数(1,2,3,4,5,6,7,8,9,10)写入到一个名字为tom.dat文件中,
 然后按相反顺序读出这些数据并显示在屏幕上。(注意,一个int型数据占4个字节)
 */import java.io.*;public class ReadFile { /**
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("D:/tom.dat");
try {
RandomAccessFile RandomAccess = new RandomAccessFile(file, "rw");
for (int i = 1; i <= 10; i++) {
try {
RandomAccess.writeInt(i);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
RandomAccess.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
RandomAccessFile randomFile = new RandomAccessFile(file, "r");
 /*
  *  for (int i = 1; i <= 10; i++) {
randomFile.seek(i);
int j = randomFile.readInt();
System.out.println(j);
} 从前往后读,正常,可以读取1,2,3,4,5,6,7,8,9,10,
但是从后往前读,结果为196608 768 3 3355 4432 131072 512 2 16777216 65536 256
  * */
try {

for (int i = 10; i >= 1; i--) {
randomFile.seek(i);
int j = randomFile.readInt();
System.out.println(j);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
randomFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }}这是运行结果
196608
768
3
33554432
131072
512
2
16777216
65536
256哪位大侠帮忙搞定一下啊

解决方案 »

  1.   

            RandomAccessFile file = null;
            try {
                file = new RandomAccessFile("example.dat","rw");
                for (int i = 1; i <= 10; i++) {
                    file.writeInt(i);
                }
            } catch (final IOException e) {
                System.err.println(e.getMessage());
            } finally {
                try {
                    if (file != null) {
                        file.close();
                    }
                } catch (final IOException e) {
                    System.err.println(e.getMessage());
                }
            }        try {
                file = new RandomAccessFile("example.dat","r");
                for (int i = 1; i <= 10; i++) {
                    System.out.println(file.readInt());
                }
            } catch (final IOException e) {
                System.err.println(e.getMessage());
            } finally {
                try {
                    if (file != null) {
                        file.close();
                    }
                } catch (final IOException e) {
                    System.err.println(e.getMessage());
                }
            }        try {
                file = new RandomAccessFile("example.dat","r");
                for (int i = 9; i >= 0; i--) {
                    file.seek(i*4L);
                    System.out.println(file.readInt());
                }
            } catch (final IOException e) {
                System.err.println(e.getMessage());
            } finally {
                try {
                    if (file != null) {
                        file.close();
                    }
                } catch (final IOException e) {
                    System.err.println(e.getMessage());
                }
            }你没看过seek的文档。按字节移动定位。提示你int是4字节。