0

I'm creating a program that can read .log files in certain directories and then dumps the data from the .log files into a local database.

However, i noticed in my testing that whenever the program reads the files and i happen to access the files during the test run - the program freezes.

How do i solve this issue?

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;
}

}

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
Ryan
  • 15
  • 1
  • 7
  • Did you use some sort of debbuger during your test run ? – Rann Lifshitz Mar 19 '18 at 03:49
  • As a side note - I'd suggest you do some refactoring of your code : add some private methods which will replace your if/else statements, and also identify recurring functionalities and move them into private methods – Rann Lifshitz Mar 19 '18 at 03:52
  • Also, you mentioned that your program freezes whenever you access the log files during the test runs. Does this mean that if you do not access the files, then the program executes successfully? – Rann Lifshitz Mar 19 '18 at 03:56
  • No, i did not use a debugger @RannLifshitz – Ryan Mar 19 '18 at 03:56
  • So far, it works smoothly whenever i don't access it. I haven't tried deleting the file when the program is in the process of reading it, but i suspect that it would also freezes the program. – Ryan Mar 19 '18 at 03:58
  • It also doesn't freezes every time i access it. it's a bit inconsistent really. – Ryan Mar 19 '18 at 04:09

1 Answers1

0

1 - Do you need to worry about the fact that it does not work when the file is open or are you just interested in why it does not work as expected ?

2 - All of the code that you wrote to determine the value for scalendar can be reduced to 2 lines of code :

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

Here is unit test

@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);

}

where the result will look similar to : 2018-03-19 06:42:58

Stellasie
  • 31
  • 3
  • 1. I need to prevent the program from freezing at all. I thought about implementing a timeout feature with ExecutorService, but the .log files can be pretty lengthy, so the reading process may vary widely. 2. Thank you for your improvement over my code – Ryan Mar 19 '18 at 05:06
  • I found https://stackoverflow.com/questions/2537306/java-opening-and-reading-from-a-file-without-locking-it this post might help. Look at the solution that mentions StandardOpenOption.READ. Please note that I did not try this myself. Hope it helps – Stellasie Mar 20 '18 at 11:22