0

我有一个 75Mb 的文本文件(.txt)。它有大约 3,00,000 行,每行大约有 100 个单词(每行 50-60 个字符)。我需要阅读大约 100 行,然后使用某种标记将其放在第 101 行的开头,处理这 100 行并清除所有数据,然后再从第 101 行再次读取,我该怎么做它?如何保留标记?

   public static void calculateFormulaValues()
    {
        System.out.println("\n\t****************************************calculateFormulaValues()****************************");
        for (CellBean cellBean : Reader.cellList)
        {
            System.out.println("\tCellName="+cellBean.getCellID()+" Time="+cellBean.getHour()+":"+cellBean.getMins());
            LinkedHashMap<String,Double> formulaValueMap=new LinkedHashMap<>();
            LinkedHashMap<String,String> headerValueMap=cellBean.getHeaderValueMap();

        kpiNameLoop:    
            for (String kpiNameToBeCalculated : Reader.kpiAndFormulaMap.keySet())
            {
                String formulaToBeEvaluated=Reader.kpiAndFormulaMap.get(kpiNameToBeCalculated);

                //System.out.println("\n*************Trying to calculate::"+kpiNameToBeCalculated+"="+formulaToBeEvaluated+"***********************************");
                if(formulaToBeEvaluated==null || formulaToBeEvaluated.trim().equals(""))
                {
                    System.err.println("\n************FORMULA-NOT-FOUND-EXCEPTION*******************");
                    System.err.println("\nCellBean-->"+cellBean);
                    System.err.println("KpiToBeCalculated-->"+kpiNameToBeCalculated);
                    System.err.println("Formula Retrieved-->"+formulaToBeEvaluated);
                    System.err.println("************************************************************");
                    System.err.flush();
                    continue kpiNameLoop;
                }
                //^[^\d\W]\w*$
                Pattern p = Pattern.compile("[a-zA-Z_#0-9]+");
                //Pattern p = Pattern.compile("^[^\\d\\W]\\w*$");
                Matcher m = p.matcher(formulaToBeEvaluated);

                ArrayList<String> myHeaders = new ArrayList<String>();
                boolean result=m.find();
                while(result) 
                {

                    //System.out.println(m.group());
                    myHeaders.add(m.group());
                    result = m.find();
                }

                //System.out.println("HEADSERS(UNSORTED)="+myHeaders);
                TreeSet<String> sortingTreeValues=new TreeSet<>(new java.util.Comparator<String>() {

                    @Override
                    public int compare(String str1, String str2)
                    {
                        if(str1.length() > str2.length())
                            return -1;
                        if(str1.length() < str2.length())
                            return +1;
                        if(str1.length() == str2.length())
                            return +1;
                        return 0;
                    }
                });

                for (String headers : myHeaders)
                {
                    if(Character.isDigit(headers.charAt(0)))
                        continue;
                    else
                        sortingTreeValues.add(headers);
                }


                //System.out.println("HEADSERS(SORTED)="+sortingTreeValues);

                for (String counterName : sortingTreeValues)
                {

                    String valueForCounter=headerValueMap.get(counterName);
                    //System.out.println("\tValue For-->"+counterName+"="+valueForCounter);

                    if(valueForCounter==null)
                    {
                        System.err.println("\n*******************************VALUE NOT-GOT EXCEPTION***************");
                        System.err.println("\nCellBean-->"+cellBean);
                        System.err.println("KpiToBeCalculated-->"+kpiNameToBeCalculated);
                        System.err.println("Formula Retrieved-->"+formulaToBeEvaluated);
                        System.err.println("CounterNotFound--->"+counterName);
                        System.err.println("******************************************************************");
                        System.err.flush();
                        continue kpiNameLoop;
                    }

                    if(valueForCounter.trim().equals(""))
                    {
                        valueForCounter="0";
                    }

                    formulaToBeEvaluated=formulaToBeEvaluated.replaceAll(counterName, valueForCounter);
                }

                //System.out.println("FORMULA AFTER REPLACING-->"+formulaToBeEvaluated);
                formulaToBeEvaluated=formulaToBeEvaluated.replaceAll("0.0/0.0", "0.0");
                formulaToBeEvaluated=formulaToBeEvaluated.replaceAll("0/0", "0");
                formulaToBeEvaluated=formulaToBeEvaluated.replaceAll("[0-9\\.]+/0\\.0", "0.0");
                //System.out.println("Formula After Handling Zeros Exception="+formulaToBeEvaluated);

                ScriptEngineManager mgr = new ScriptEngineManager();
                ScriptEngine engine = mgr.getEngineByName("JavaScript");
                String finalValue="";
                try {
                        String resultValue=engine.eval(formulaToBeEvaluated).toString();
                        finalValue=resultValue;


                    }catch (ScriptException e) 
                    {
                        // TODO Auto-generated catch block"40+2"
                        System.out.println("************* ERROR in PARSING Formula******************");
                        System.out.println("FormulaCreated-->"+formulaToBeEvaluated);
                        System.out.println(e);
                        e.printStackTrace();
                        System.out.flush();
                    }

                /*System.out.println("\n************************************FormulaHasBeenCalculated*******************************");
                System.out.println(kpiNameToBeCalculated+"="+formulaToBeEvaluated+"="+finalValue);
                System.out.println("*********************************************************************************************");*/
                formulaValueMap.put(kpiNameToBeCalculated,roundOff(new Double(finalValue),3));

            }

            cellBean.setKpiFormulaValueMap(formulaValueMap);
        }
    }
4

0 回答 0