本帖最后由 李东梁 于 2014-3-25 16:48 编辑
- public static void readFile2() throws IOException {
-
- FileInputStream fis = new FileInputStream("temp\\fos.txt");
-
- // System.out.println(fis.available());//获取文件的字节数。
- // byte[] buf = new byte[fis.available()];//以它作为缓冲区的长度是不合适,容易造成内存溢出
- // fis.read(buf);
- // System.out.println(new String(buf));
- //创建一个缓冲区。缓冲读到的字节。
- byte[] buf = new byte[1024];
- int len = 0;
- while((len=fis.read(buf))!=-1){
- System.out.println(new String(buf,0,len));
- }
-
- fis.close();
- }
- 上面讲到不能用fis.available();做为缓冲区的长度,应为是内存溢出。
- 这个内存是什么内存?
- byte[] buf = new byte[fis.available()];
- byte[] buf = new byte[1024];
- 这两个有什么区别?
- 急!!!
复制代码
|