我正在使用谷歌翻译,我希望这个问题很好理解。
有一件事我不了解随机访问文件。不了解程序是如何工作的,但它可以工作。
这是我的程序:
// ---------------------------------------------
RandomAccessFile RandomAccessFile = new RandomAccessFile ( " pathfile ", " r");
byte [] document = new byte [ ( int) randomAccessFile.length ()] ;
randomAccessFile.read (document) ;
// ---------------------------------------------
在第 1 行中,我在 read 中访问文件 在第 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 ) .
只返回字节数而不是字节数。
有人可以向我解释这一行如何使用此语句转储 byte [] 变量文档中的字节?
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 ();
刚刚阅读,内容不会在任何地方通过该行...