Rajesh Murugan
Rajesh Murugan

Reputation: 159

Listing filenames with Unicode Characters (Arabic)

I am trying to get all filenames in a directory and subdirectory in it. The name of the directory and files are in Arabic. When I get the name of the file it is returning ???? mark instead of the name. Did anyone face this issue earlier?

    boolean recursive = true;
    Collection files = FileUtils.listFiles(root, null, recursive);

    //System.out.println(files.toString());

    PrintWriter success = new PrintWriter("E:\\success.txt", "UTF-8");

    for (int i = 0; i < list.size(); i++) {
        fileName = list.get(i);
        for (Iterator iterator = files.iterator(); iterator.hasNext();) {
            File file = (File) iterator.next();
            if (file.getName().endsWith(fileName)) {
                Files.copy(Paths.get(file.getAbsolutePath()),
                        Paths.get(targetDirectory + "\\" + file.getName()));
                success.println(fileName.toString());
            }
        }

    }

Upvotes: 0

Views: 721

Answers (1)

shakhawat
shakhawat

Reputation: 2727

Have you tried UTF-16 encoding format?

PrintWriter success = new PrintWriter("E:\\success.txt", "UTF-16");

Edit: I think UTF-8 should be able to encode Arabic character properly. The problem here might be the text editor you are using to read the file. Please check the encoding format of the text editor you are using to read your file. Setting the encoding to Unicode UTF-8 in the editor should solve your problem.

Upvotes: 2

Related Questions