首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

没看懂API,来问问,该怎么解决

2012-01-19 
没看懂API,来问问File file new File()FileReader reader new FileReader(file)char[] buf new

没看懂API,来问问
File file = new File("");
  FileReader reader = new FileReader(file);
  char[] buf = new char[(int)file.length()];  
  int len = reader.read(buf);
  String results = new String(buf,0,len);
  System.out.print(results);
定义了一个buf数组长度为file.length,里面应该没有内容
reader.read(buf)是什么意思?
读文件的内容放到buf这个数组中,然后返回数组的长度?

[解决办法]
file.length是文件的字节长度
reader.read(buf);是把文件按字符的方式读入到buf数组中
返回成功读入的字符数,所以和file.length(也就是数组的长度)可能有出入,因为对于多字节字符,字符数和字节数不一样

热点排行