0

I seem to be having a randomly occurring issue that I hope someone can shed light on. I am reading in a file and outputting the contents. I have one test file, which can be read in 90% of the time. However, once in a while it incorrectly reports that the test file has 0 lines. Is there a reason this is happening? I would expect that it always reads in the contents. A bare bones example is shown below:

public void foo (Path path){
    System.out.println("Exists?"+ Files.exists(path)); // Always says true
    List<String>lines = Files.readAllLines(path);
    System.out.println("NumLines:"+ lines.size()); // Sometimes 0, sometimes 7
}

I have also tried this with a BufferedReader(new FileReader(path.ToFile())) and get the same results

ekjcfn3902039
  • 1,673
  • 3
  • 29
  • 54
  • Where is the file located? A local drive or a network share? – Novaterata Apr 28 '17 at 13:04
  • It's on my local drive. And it's a 5kb file, so not much in it – ekjcfn3902039 Apr 28 '17 at 13:14
  • Could you disable any anti-virus and try to reproduce? – Novaterata Apr 28 '17 at 13:28
  • Unfortunately, I don't believe it has anything to do with that. – ekjcfn3902039 Apr 28 '17 at 13:35
  • Is this OpenJDK or Oracle? Might need to file a bug for it or update your JDK/JRE Since it's IO related it could even be related to the disk controller driver. There really are a lot of factors involved. – Novaterata Apr 28 '17 at 13:44
  • similar http://stackoverflow.com/questions/22652809/system-io-file-readalllines-returns-zero-strings Is this a file that you create prior to running this method? If so then you'd need to make sure you flush any write operations as that will definitely be non-deterministic. Any details like that would be helpful, as this stands there is not enough information. – Novaterata Apr 28 '17 at 13:49
  • this code should not compile - unhandled IOException - and that could make a difference. – user85421 Apr 28 '17 at 14:16
  • I've got Oracle v1.8.0_66 . The code was just a quick hand-written example... you can throw an IOException to the method if you need it to compile – ekjcfn3902039 Apr 28 '17 at 14:39
  • When you get zero, try opening the file in some other tool, like a text editor. Also try adding code that prints the file’s overall size, such as `System.out.println(Files.size(path));`. – VGR Apr 28 '17 at 15:19
  • And since my last comment I can't replicate it anymore (even though it's been happening for a few days). Law of irony... – ekjcfn3902039 Apr 28 '17 at 17:39

0 Answers0