本帖最后由 Super_Class 于 2013-5-25 11:27 编辑
- public static void read3(File file) throws Exception{
- FileInputStream fis = new FileInputStream(file);
- byte []buf = new byte[1024];
- int b = 0;
- int count = 1;
- while((b = fis.read(buf) )!=-1)
- if(b == '\n')
- count++;
- fis.close();
- System.out.println(count);
- }
复制代码 用这个代码不论是读哪个都是1.
但是用以下两个代码都没有问题- public static void read1(File file)throws Exception{ //这个是每次读一个字符
- FileInputStream fis = new FileInputStream(file);
- int b = 0;
- int count = 1;
- while((b = fis.read()) != -1){
- if(b == '\n')
- count++;
- }
- fis.close();
- System.out.println(count);
- }
复制代码 以下这个是一下都读进内存中,
- public static void read2(File file){
- try{
- FileInputStream fis = new FileInputStream(file);
- BufferedInputStream bis = new BufferedInputStream(fis);
- int b = 0;
- int count = 1;
- while((b = bis.read())!=-1)
- if(b == '\n')
- count++;
- fis.close();
- bis.close();
- System.out.println(count);
- }catch (Exception e) {
- System.out.println(e.getMessage());
- }
复制代码 |