当前位置:编程学习 > JAVA >>

从网上看到一个java题,没事练练,搞不定。

题目和我写的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



哪位大侠帮忙搞定一下啊 --------------------编程问答--------------------
        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());
            }
        }

引用
RandomAccessFile can be treated as a huge byte array. You can use a file pointer (of type long), similar to array index, to access individual byte or group of bytes in primitive types (such as int and double). The file pointer is located at 0 when the file is opened. It advances automatically for every read and write operation by the number of bytes processed.


你没看过seek的文档。按字节移动定位。提示你int是4字节。 --------------------编程问答--------------------
引用楼主 liuxvwei06 的回复:
题目和我写的java代码如下:

/*编写一个Java应用程序,利用RandomAccessFile类,把几个int型整数(1,2,3,4,5,6,7,8,9,10)写入到一个名字为tom.dat文件中,
 然后按相反顺序读出这些数据并显示在屏幕上。(注意,一个int型数据占4个字节)
 */

import java.io.*;

public class ReadFile {
……

+1
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,