randomAccessFile方法不能写入或读取整数?

时间:2017-03-23 20:15:13

标签: java

我有两种方法,一种是向randomAccessFile写入10个int,另一种是读取10个int。我相信编写器方法没有按预期运行。

这是我写入随机文件的方法:

library(data.table)

dt <- data.table(A = 1:3, B = letters[1:3], foo = paste0("bar", "baz", "bee"))

oldNames.1 <- names(dt)
oldNames.2 <- copy(names(dt))

setnames(dt, c("A", "B"), c("notA", "notB"))

newNames <- names(dt)

newNames == oldNames.1
[1] TRUE TRUE TRUE

newNames == oldNames.2
[1] FALSE FALSE  TRUE

以下是我阅读这些内容的方法:

try{
                RandomAccessFile file = new RandomAccessFile(nextPath, "rw");
                System.out.println("Writing these ints to file");
                file.seek(0);
                // here I loop 10 times and write the int at position i
                for (int i = 0; i < 10; i++)
                {
                    file.seek(i);
                    toAdd = randInt(1,10);
                    file.writeInt(toAdd);
                    System.out.printf(toAdd + " ");

                }
                file.seek(0);
                System.out.println("");
            }catch(IOException ex){
                System.out.println("Error");
                System.exit(0);}

这是我的输出:为什么只读取最后一个int?

public int[] readRandom()
    {
        System.out.println("Unsorted ints found in random file:");
        int[] randomInts = new int[10];
        try{        
                RandomAccessFile file = new RandomAccessFile(nextPath, "rw");
                file.seek(0);
                // here I loop 10 times, and read the Int at position i
                for(int i = 0; i < 10; i++)
                {
                    file.seek(i);
                    randomInts[i] = file.readInt();
                    System.out.printf(randomInts[i] + " ");


                }   
                file.seek(0);
                System.out.println("");
            }catch(IOException exc){
                System.out.println("Error reading ints");
                System.exit(0);}
                return randomInts;
            }

2 个答案:

答案 0 :(得分:1)

当你在做file.seek(i)时,你错过了一个重要的观点。 seek()方法寻找你提供的字节位置,并且你将位置递增到1.但是当你将整数写入文件时,它会为每个整数写入4个字节。你的柜台应该是这样的

for (int i = 0; i < 10; i++) {
    file.seek(i*4);
    toAdd = randInt(1,10);
    file.writeInt(toAdd); 
    System.out.printf(toAdd + " ");
 }

显然,对于阅读,你这样做

for (int i = 0; i < 10; i++)
{
    file.seek(i*4);
    randomInts[i] = file.readInt();
    System.out.printf(randomInts[i] + " ");

}

答案 1 :(得分:1)

为什么使用RandomAccessFile执行顺序写入和顺序读取?它违背了这门课的目的 此外,你不应该震惊被捕获的异常,而是记录或追踪它们。

关于您的意外结果,它是由不需要的seek()调用引起的。 您不需要在每次读取或写入操作时调用seek() readInt(),而writeInt()会使光标前进。

我简化了您的示例代码,以强调重要部分:

public void write() {
    try {
        RandomAccessFile file = new RandomAccessFile("temp.txt", "rw");
        System.out.println("Writing these ints to file");
        file.seek(0);
        for (int i = 0; i < 10; i++) {              
            file.writeInt(i);
            System.out.printf(i + " ");
        }           
    } catch (IOException ex) {
        ex.printStackTrace();
        System.exit(0);
    }
}

public int[] readRandom() {
    System.out.println("Unsorted ints found in random file:");
    int[] randomInts = new int[10];
    try {
        RandomAccessFile file = new RandomAccessFile("temp.txt", "rw");
        long filePointer = file.getFilePointer();

        file.seek(0);
        for (int i=0; i<10; i++){
            randomInts[i] = file.readInt();
            System.out.printf(randomInts[i] + " ");
        }               
    } catch (IOException ex) {
        ex.printStackTrace();
        System.exit(0);
    }
    return randomInts;
}