0

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?

1
  • 1
    The problem is that you need to use the proper input encoding, so the string is correct. Check with a debugger that input contains exactly what you expect when set. You will probably be surprised. Commented Dec 4, 2017 at 0:41

1 Answer 1

1

Precisely, you don't need a file encoding, but an input encoding. Therefore you can specify a charset for your Scanner using the Scanner(InputStream source, String charsetName) constructor:

Scanner scanner = new Scanner(System.in, "UTF-8");
Sign up to request clarification or add additional context in comments.

2 Comments

sorry, but nothing has changed.
@Nosairat Try piping your input from a text file that you encode in UTF-8. It's likely that the encodings for your terminal and Scanner are not in alignment or don't support the characters that you are trying to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.