This is weird but I don't know why it being caused.
I'm using Java 10 and trying to read a file. Sometime the file is read and the file's content is outputted to the console however sometimes it doesn't output the content at all.
I've confirmed that it can read and write the file and something is in the string variable I use but whatever it returns it prevent anything from being printed to the console sometimes.
It sometimes works the first few times then fails the next ten. All the other code is working as expected except when it interacts with this hidden variable. No exception is thrown from it.
Here is the code to test with.
public String getText(String filePath) {
File file = new File(filePath);
System.out.println(file.getAbsolutePath());
System.out.println(file.exists());
System.out.println(file.canRead());
String text = null;
System.out.println("try getting file content");
try {
byte[] array =Files.readAllBytes(file.toPath());
System.out.println(""+array.length);
text = new String(array, StandardCharsets.UTF_8);
}catch(Exception e) {
System.out.println(e.getMessage());
}
if(text != null) {
System.out.println("file content have been retrieved successfully");
System.out.println("["+text+"]"); //this line won't print sometimes
}
return text;
}