如果程序无法读取文件,如何继续循环的下一次迭代

时间:2018-03-19 03:37:29

标签: java file loops

我创建的程序可以读取某些目录中的.log文件,然后将.log文件中的数据转储到本地数据库中。

但是,我在测试中注意到,每当程序读取文件并且我碰巧在测试运行期间访问文件时 - 程序会冻结。

我如何解决这个问题?

public static void file(File[] files)
{
    try
    {    
        for (File lister : files)
        {
            System.out.println("HERE " + lister);

            in = new FileReader(lister);
            br = new BufferedReader(in);

            try 
            {
                    while ((sCurrentLine = br.readLine()) != null)
                    {
                        if (sCurrentLine.contains("Performance"))
                        {
                            String[] aCurrentLine = sCurrentLine.split("\\|");
                            if (aCurrentLine.length >= 6) {
                                Date date = dateinsert.parse(aCurrentLine[0]);
                                CurrentTime = dateinsert.format(date);
                                CurrentFlow = aCurrentLine[2];
                                CurrentModule = aCurrentLine[5];
                                CurrentType = aCurrentLine[4];
                                sCurrentID = aCurrentLine[6];
                                aCurrentLine = aCurrentLine[6].split("ORDER_ID");

                                if (aCurrentLine.length >= 2)
                                {
                                    aCurrentLine[1] = aCurrentLine[1].replace (":", "");
                                    aCurrentLine[1] = aCurrentLine[1].replace (" ", "");
                                    aCurrentLine[1] = aCurrentLine[1].replace ("_", "");
                                    aCurrentLine[1] = aCurrentLine[1].replace ("{", "");
                                    aCurrentLine[1] = aCurrentLine[1].replace ("}", "");
                                    aCurrentLine = aCurrentLine[1].split ("\"");
                                    sCurrentID = aCurrentLine[2];
                                }
                                else // Happens when there's no order id
                                {
                                    sCurrentID = "N/A";
                                }


                                cal = Calendar.getInstance();

                                year = cal.get(Calendar.YEAR);
                                month = cal.get(Calendar.MONTH);
                                datenum = cal.get(Calendar.DAY_OF_MONTH);
                                hour = cal.get(Calendar.HOUR_OF_DAY);
                                minute = cal.get(Calendar.MINUTE);
                                second = cal.get(Calendar.SECOND);

                                if (month<9)
                                {
                                    month = month + 1;
                                    smonth = "0" + Integer.toString(month);
                                }
                                else
                                {
                                    month = month + 1;
                                    smonth = Integer.toString(month);
                                }

                                if (datenum<10)
                                {
                                    sdatenum = "0" + Integer.toString(datenum);
                                }
                                else
                                {
                                    sdatenum = Integer.toString(datenum);
                                }

                                if (hour<10)
                                {
                                    shour = "0" + Integer.toString(hour);
                                }
                                else
                                {
                                    shour = Integer.toString(hour);
                                }

                                if (minute<10)
                                {
                                    sminute = "0" + Integer.toString(minute);
                                }
                                else
                                {
                                    sminute = Integer.toString(minute);
                                }

                                if (second<10)
                                {
                                    ssecond = "0" + Integer.toString(second);
                                }
                                else
                                {
                                    ssecond = Integer.toString(second);
                                }

                                scalendar = Integer.toString(year) + "-" + smonth + "-" + sdatenum + " " + shour + ":" + sminute+ ":" + ssecond;

                                ls.insertdata(sCurrentID, CurrentTime, CurrentFlow, CurrentModule, CurrentType, scalendar);
                            }
                        }
                    }
        }
    } catch (Exception e) {
        e.printStackTrace();

    }
    flag = 0;
}

}

1 个答案:

答案 0 :(得分:0)

1 - 您是否需要担心文件打开时它不起作用或您只是为什么它不能按预期工作?

2 - 您为确定 scalendar 的值而编写的所有代码可以减少为2行代码:

Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(Calendar.getInstance().getTime());

这是单元测试

@Test
public void quickTest() {
    Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String formattedDate = formatter.format(Calendar.getInstance().getTime());
    System.out.println(formattedDate);

}

其结果类似于:2018-03-19 06:42:58