当前位置:  技术问答>java相关

送分11

    来源: 互联网  发布时间:2015-01-21

    本文导语:  You need to read in the lines of a large text file containing tens of megabytes of data. Which of the following would be most suitable for reading in such a file  1) new FileInputStream("file.name")  2) new InputStreamReader(new FileInputStream("...

You need to read in the lines of a large text file containing tens of megabytes of data. Which of the following would be most suitable for reading in such a file 

1) new FileInputStream("file.name") 
2) new InputStreamReader(new FileInputStream("file.name")) 
3) new BufferedReader(new InputStreamReader(new FileInputStream("file.name"))); 
4) new RandomAccessFile raf=new RandomAccessFile("myfile.txt","+rw"); 

选什么?为什么?

|
我再来试试. 
InputStreamReader是个byte到char过渡型,开了个8192的buffer只是为了转化byte到char.
read需要的空间有调用者分配.
BufferedReader的defaultbuffersize=8192,当一次read 操作大于8192,就会重新分配空间,但是问题的是文件是由n行(8k以上)的字符组成,还是有很多行(一行小于8k)组成.
FileInputStream只有一个FileDescriptor的成员变量,那么read的时候,空间由调用者自己分配,但是题目是文本文件基于char的,stream读出来还是基于byte的,显然不适合.
RandomAccessFile 其实类似FileInputStream,空间由调用者分配.但没有"+rw"这个mode参数.
如果file.name是一个有很多行大于8k的文件,那么答案是2



|
我认为可能是 4 吧。
因为前面几个对字节流/字符流操作的类可能都会试图将读进的内容保存在内存中的缓冲区中,在文件太大的情况下是不合适的。

    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3