Most efficient way to check if a file is empty in Java on Windows

后端 未结 12 1194
南笙
南笙 2020-12-05 04:30

I am trying to check if a log file is empty (meaning no errors) or not, in Java, on Windows. I have tried using 2 methods so far.

Method 1 (Failure)

12条回答
  •  [愿得一人]
    2020-12-05 05:33

    String line = br.readLine();
    String[] splitted = line.split("anySplitCharacter");
    if(splitted.length == 0)
        //file is empty
    else
        //file is not empty
    

    I had the same problem with my text file. Although it was empty, the value being returned by the readLine method was not null. Therefore, I tried to assign its value to the String array which I was using to access the splitted attributes of my data. It did work for me. Try this out and tell me if it works for u as well.

提交回复
热议问题