0

当我尝试填充随机访问文件时遇到问题,在使用循环时它会在达到第 924 个字节时停止写入,所以当我尝试用“-”填充 2500 个字节时,它只填充 924字节。这是我使用的代码,希望有人能告诉我我做错了什么

public class Hashing {
    private RandomAccessFile espacio;

    public Hashing() throws FileNotFoundException, IOException{
        File amigos = new File("Texto");
        String r = "rws";
        espacio = new RandomAccessFile(amigos ,r);
        this.estableceEspacioLlenado();
    }

    public void estableceEspacioLlenado() throws IOException{
        int a = 0;

        while(a<2500){
            if(a>=241 && a%241==0){
                espacio.writeBytes("\n"); 
                a++;
            }
            else{
                espacio.writeBytes("-");
                a++;
            }
        }
    }        
}

输出文件:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4

1 回答 1

3

也许数据被缓冲并且没有被刷新到磁盘。在最后添加一个close()调用以确保所有内容都被写入。

espacio = new RandomAccessFile(amigos ,r);
this.estableceEspacioLlenado();
espacio.close();
于 2012-11-28T04:55:39.197 回答