7

I want to createNewFile with a path but I got an IOException. The question is, the detailed message cannot be interpreted, I can only see a bunch of question marks.

enter image description here

I am with Windows 10 originally in Spanish, but with Chinese language pack installed. The java language already set to en and file encoding UTF-8:

java -version
Picked up _JAVA_OPTIONS: -Duser.country=US -Duser.language=en -Dfile.encoding=UTF-8
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)

Why? Only this exception cannot be read.

EDIT: tried to define the language as zh, but does not work.

Use this main class to reproduce it:

public class PromotionTargetFileHandlerMain {
    public static final String uploadingDir = String.join(File.separator,
            System.getProperty("user.dir"), "targets_csv");
    private static final File destDir = new File(uploadingDir);

    public static void main(String[] args) {
        createFileDestination("target.csv");
    }

    public static void createFileDestination(String filename) {
        if (!destDir.exists()) {
            try {
                Files.createDirectory(Path.of(uploadingDir));
            } catch (FileAlreadyExistsException e) {
                log.trace("File dir already exists: {}", uploadingDir);
            } catch (IOException e) {
                log.error("Cannot create temp file dir {}", uploadingDir, e);
                throw new RuntimeException(e);
            }
        }
        String saveLocation = String.join(File.separator,
                uploadingDir, filename
        );
        File saveFile = new File(saveLocation);
        if (saveFile.exists()) saveFile.delete();
        try {
            saveFile.createNewFile();   // <--------------- here IOException
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
WesternGun
  • 11,303
  • 6
  • 88
  • 157
  • OK maybe duplicate for: https://stackoverflow.com/questions/17159357/is-there-a-way-to-force-exception-message-to-be-english-for-java-1-7. Seems that it is an OS level message, which is affected by language pack(so it is in Chinese), but then decoded with another locale...? – WesternGun Aug 28 '20 at 11:08
  • Can you [edit] your question and post a [mcve]? Also, how are you running your java code? Are you running it from an IDE? If so, which one? Or are you running it from a command prompt window? Are you running a single java class or a JAR file? – Abra Aug 28 '20 at 12:06
  • Hi Abra, I will try to edit it; I am running it from Intellij IDEA, it is a microservice with Spring Boot with an API to upload file. I run it with Gradle. – WesternGun Aug 28 '20 at 12:10
  • You don't need to post all your code. You just need to post code that reproduces your problem, i.e. the code that causes the `IOException`. You can't read the message, but it appears that the stack trace is readable. So locate which line of your code is throwing the exception and try to create a [mcve] from that. – Abra Aug 28 '20 at 12:18
  • 1
    [edit] your question, not some repository. – Holger Aug 28 '20 at 12:36
  • BTW what I want to know, is how to change encoding of this OS level message so that I can read it; it is not about "troubleshooting why I cannot create a file", but "generic solution of configuration in Java side to read language specific IO error message in Windows" – WesternGun Aug 28 '20 at 13:12
  • If you add `System.out.println(e.getMessage().codePointAt(0));` to your last catch block, before the `throw new RuntimeException(e)`, what does it print? – VGR Aug 28 '20 at 21:28
  • 63, value of `?` – WesternGun Aug 30 '20 at 13:13
  • And duplicate of this: https://stackoverflow.com/questions/17159357/is-there-a-way-to-force-exception-message-to-be-english-for-java-1-7 – WesternGun Aug 31 '20 at 09:28

2 Answers2

0

I may found one solution: change locale in Windows 10 for non-Unicode applications in Control Panel. It was Spanish, but now I changed to Chinese, then I can see some localized message of other IO errors(cannot reproduce the same error in the question):

enter image description here

I found this:

...and .NET exception messages will be localized regardless of the intended recipient of the message.

So it leads me to believe that the process is like this:

  • native OS level error message is always in Chinese
  • and, due to OS locale setting for non-Unicode(why Java is considered non-Unicode application, I don't know), it wants to interprete it as Spanish, but failed
  • so I can only see ??????

In Windows 10, you can type "Control Panel" in the search bar, and go to: Region -> Management(tab) -> non-Unicode application language, set to Chinese(or others as in your local language pack), and restart.


Of course, it affects other non-Unicode application, too.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
-4

Write your code in try catch blog and in catch (Exception e) then if your file objecct has no issues then you will get the results

  try{
//code
}catch(Exception e){

println("Exception"+e)
}