My basic code is trying to check files exitence, based on paths, but it can't deal with Unicode Characters:
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
File f = new File(input);
if (f.exists()) {
System.out.println("File is Found, According to java.io");
} else {
System.out.println(f.toString() + " is Not Existed, According to java.io ");
}
Path x = Paths.get(input);
if (Files.exists(x)) {
System.out.println("File is Found, According to java.nio");
} else {
System.out.println(x.toString() + " is Not Existed, According to java.nio");
}
when the input (ie. file path) is in ASCII, the code works fine, but when the input contains UTF-8 chars, the code fails in both :
1- printing the input properly.
2- determining does the file exist (ie. even when the file exists, the code tells that file is not exited)
Example:
input:
c://€.jpg
output:
c:\�.jpg is Not Existed, According to java.io
c:\�.jpg is Not Existed, According to java.nio
I use NetBeans, Java 1.8, maven.
PS: I tried to use:
run with :
-Dfile.encoding=UTF-8
add the followign to project properties :
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
, but nothing happened.
Any help, please?
inputcontains exactly what you expect when set. You will probably be surprised.