Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.isFile() returns incorrect result? [duplicate]

Tags:

java

file

public class Test {
  public static void isFile() {
    System.out.println(new File("D:\\a.log").isFile());
  }

  public static void main(String[] args) {
    System.out.println(new File("‪D:\\a.log").isFile());
    isFile();
  }
}

The expected result is:

true
true

but actually the result is:

false
true

The file D:\a.log is actually exist and the path is correct, the jdk version is 1.8.0_11, and the os is win10

How to understand this??

like image 525
winflex Avatar asked Dec 17 '22 21:12

winflex


1 Answers

The string "‪D:\\a.log" inside the main method has 9 characters, the one in the isFile method has 8 characters.

The one inside the main method starts with invisible unicode character U+202A ("LEFT-TO-RIGHT EMBEDDING").

Replace the string inside main with the string inside isFile, or delete the first invisible character from the string in main.

(Note: this shows the real benefit of copy-pasting your actual code, as the problem is in the code that you pasted in your question above)

like image 105
Erwin Bolwidt Avatar answered Dec 29 '22 12:12

Erwin Bolwidt