java - RandomAccessFile 类如何使用 randomAccessFile.read() 方法返回字节;

标签 java arrays randomaccessfile

我正在使用谷歌翻译器,我希望这个问题很好理解。

有一件事我不明白随机访问文件。不明白这个程序是如何工作的,但它确实有效。

这是我的程序:

// ---------------------------------------------
RandomAccessFile RandomAccessFile = new RandomAccessFile ( " pathfile ", " r");

byte [] document = new byte [ ( int) randomAccessFile.length ()] ;

randomAccessFile.read (document) ;
// ---------------------------------------------

在第 1 行中,我以读取方式访问文件 在第 2 行中,我创建了一个与文件大小相同的字节数组对象 在第 3 行读取字节数组

但永远不会转储字节数组上的文件。

我认为该程序应该类似于:

/ / ---------------------------------------------
RandomAccessFile RandomAccessFile = new RandomAccessFile ( " pathfile ", " r");

byte [] document = new byte [ ( int) randomAccessFile.length ()] ;

// Line changed
document = randomAccessFile.read();
// ---------------------------------------------

Java 文档说:

randomAccessFile.read() ;

Reads a byte of data from this file . The byte is returned as an integer in
the range 0 to 255 ( 0x00- 0x0ff ) .

只返回字节数,不返回字节数。

有人可以向我解释这一行如何使用此语句转储字节 [] 变量文档中的字节吗?

randomAccessFile.read (document) ;

谢谢!!

//------------------------------------------ ----------------------------------

另一个例子:

我将此方法与 BufferedReader 进行比较:

File file = new File ("C: \ \ file.txt"); 
FileReader fr = new FileReader (file); 
BufferedReader br = new BufferedReader (fr); 
... 
String line = br.readLine (); 

BufferedReader 读取一行并将其传递给一个字符串。

我可以看到这个将文件内容传递给变量的 java 语句。

String line = br.readLine ();

但我没有看到其他声明:

RandomAccessFile.read ();

只是阅读,内容不会在任何地方通过那条线......

最佳答案

你应该使用 readFully

    try (RandomAccessFile raf = new RandomAccessFile("filename", "r")) {
        byte[] document = new byte[(int) raf.length()];
        raf.readFully(document);
    }

编辑:您已经澄清了您的问题。您想知道为什么 read 不“返回”文件的内容。内容如何到达那里?

答案是read不分配任何内存来存储文件的内容。您使用 new byte[length] 做到了这一点。这是文件内容所在的内存。然后您调用 read 并告诉它将文件的内容存储在您创建的这个字节数组中。

BufferedReader.readLine 不是这样操作的,因为只有它知道每一行需要读取多少字节,所以让你自己分配它们是没有意义的。

“如何”的快速示例:

class Test {
    public static void main(String args[]) {
        // here is where chars will be stored. If printed now, will show random junk
        char[] buffer = new char[5];

        // call our method. It does not "return" data.
        // It puts data into an array we already created.
        putCharsInMyBuffer(buffer);

        // prints "hello", even though hello was never "returned"
        System.out.println(buffer);
    }

    static void putCharsInMyBuffer(char[] buffer) {
        buffer[0] = 'h';
        buffer[1] = 'e';
        buffer[2] = 'l';
        buffer[3] = 'l';
        buffer[4] = 'o';
    }
}

关于java - RandomAccessFile 类如何使用 randomAccessFile.read() 方法返回字节;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22375924/

相关文章:

java - Swing 客户端上的 Apache Shiro - EJB 3.1 身份验证授权案例

java - 如何正确关闭MappedByteBuffer?

java - 如何写入 Java 中的 txt 文件中的特定行号

java - 使用java从大文件中读取 block

java - 如何从另一个类设置 TextView (Android)

java - Jetty/Shiro 初始化错误 2013-06-01 03 :37:23. 574 :WARN:oejj. ObjectMBean:main:

java - 全局序列比对中的回溯

java - 无法从双 HashMap 中检索

java - 使用 for-each 循环或使用迭代器迭代 JSON 数组 (Android Studio)

arrays - 如何解决 Java heap space Exception for ArrayList Size?