java文件io性能测试疑问

1 使用file reader测试随机io时间。
实验非预期结果:
a 当我修改了small.txt文件内容时,下一个循环依然是旧的内容,new,read操作读的是缓存??
b 当只执行一次read操作时,执行时间不足1ms,比想象中的快。据说磁盘寻道时间需要几毫秒??

[code="java"]@Test
public void testSeek() throws IOException {
int num = 100;
char[] cbuf = new char[12 * 10];
long start = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
FileReader fr = new FileReader("small.txt");
// System.out.println(System.currentTimeMillis() - start);

        fr.read(cbuf);
        fr.close();

        System.out.println(new String(cbuf).toString());

    }

    System.out.println(System.currentTimeMillis() - start);

}[/code]

有人能回答我的问题吗

IO操作,磁盘的内容不是直接到java用户的进程中的,这样的话就太慢了。
当java进程请求IO操作的时候,它执行调用把控制权交给内核,内核先会看内核空间的缓冲区内有没有数据,有就直接读出来,没有的话就调用磁盘控制器去磁盘读取数据,读取会先读取到内核空间,然后再到用户进程中。内核这部分操作显然是java api调用native本地方法(也就是C语言或者C++)。
这是一些操作系统的知识,LZ可以参考一下Reilly的java nio那本书,上面阐述的很详细。

FileReader 继承自InputStreamReader

[quote]
Each invocation of one of an InputStreamReader's read() methods may

  • cause one or more bytes to be read from the underlying byte-input stream.
  • To enable the efficient conversion of bytes to characters, more bytes may
  • be read ahead from the underlying stream than are necessary to satisfy the
  • current read operation.[/quote]

InputStreamReader自带缓存

我测试了一下,第一次读一个新文件时确实是10ms~40ms,再次执行就0毫秒了;
估计是受磁盘预读和系统缓存的影响。
[url]http://simpleframework.net/blog/v/8486.html[/url]