我的代码中没有使用BufferedReader的mark(0)和reset()

时间:2016-05-23 11:58:12

标签: java

在下面的代码中,我使用mark(0)和reset()方法从上到下读取文本文件5次。但不工作请帮助我。

 package com.narasimha;
 import java.io.*;
 import java.util.*;
 public class Attempts {

 public static void main(String args[]) {

     int count=0;
     BufferedReader file;
     HashSet<String> ualist;
     List<String> salist,ssclist,splist;
     String ualine,saline,sscline,spline;
     long sum=0;
     double avg;

    try{    
        file=new BufferedReader(new FileReader("KAR_UBONA_UBONACT15_20150929_20150930_FEEDBACK.txt"));
        file.mark(0);

    //1.No of Attempts
        while(file.readLine()!=null){
            count++;
        }
          System.out.println("No of Attempts\t"+count);
         file.reset();
          count=0;
    //2. No of uniq Attempts
          file.mark(0);
          ualist=new HashSet<String>(); 
              while((ualine=file.readLine())!=null){
                    String array[]=ualine.split(",");
                    ualist.add(array[0]);
                  }
                  for(String ua:ualist){
                        count++;
                   }
            System.out.println("No of Uniq Attempts\t"+count);
             file.reset();
            count=0;
    //3. Successful Attempts
           file.mark(0);
           salist=new ArrayList<String>();
                while((saline=file.readLine())!=null){
                    String array[]=saline.split(",");
                        if(!(array[1].equals("0")))
                            salist.add(array[1]);
                            sum=sum+Integer.parseInt(array[1]);
                    }
                   for(String sa:salist){
                       count++;
                   }
                   avg=sum/count;
            System.out.println("No of Successful  Attempts\t"+count);
            //Average Mou of Successful Calls
            System.out.println("Avg Mou of Successful Calls\t"+avg);
             file.reset();
            count=0;
    //4.Song Selected Calls
           file.mark(0);
           ssclist=new ArrayList<String>();
                while((sscline=file.readLine())!=null){
                    String array[]=sscline.split(",");
                        if(!(array[2].equals("\\N")))
                            ssclist.add(array[2]);
                    }
                   for(String ssc:ssclist){
                       count++;
                   }
        System.out.println("No of Song Selected Calls\t"+count);
         file.reset();
        count=0;
    //5.calls where atleast one song was played
         file.mark(0);
           splist=new ArrayList<String>();
                while((spline=file.readLine())!=null){
                    String array[]=spline.split(",");
                        if(!(array[3].equals("")))
                            splist.add(array[3]);
                    }
                   for(String sp:splist){
                       count++;
                   }
        System.out.println("Songs where atleast on song was played\t"+count);

          file.close();

    }catch(IOException ioe){
            System.out.print("Sorry! Unable to connect to the file");
        }       
    }

}

2 个答案:

答案 0 :(得分:1)

问题是对方法<?php namespace App; use Illuminate\Auth\Authenticatable; use Illuminate\Database\Eloquent\Model; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; class Subscriber extends Model implements AuthenticatableContract, CanResetPasswordContract { use Authenticatable, CanResetPassword; protected $fillable = [ 'firstname', 'lastname', 'email', 'pass', 'phone', 'address', 'city', 'state', 'zip' ]; protected $hidden = ['pass', 'remember_token']; public function getAuthPassword() { return $this->pass; } } 的参数含义的误解。正如javadoc所解释的那样,要指定的数字是在保留标记的同时可以读取的字符数。此数字还定义缓冲区的新大小(如果它更小)。因为缓冲区在堆中,所以应该仔细使用它并适用于合适的情况(而你的缓冲区似乎不是)。

如果您必须多次扫描文件,则应多次执行打开/读取/关闭循环。

答案 1 :(得分:0)

您可以在while循环中轻松完成这项工作:

String line = null;
while((line = file.readLine()) != null) { //this gives line the String of one line of the file
    String[] array = line.split(",");
    ualist = new HashSet<String>();
    ualist.add(array[0]);
    salist = new ArrayList<String>();
    salist.add(array[1]); 
    // and so on

然后,您可以指定导出平均值的单独方法,等等。