0

See my previous question: Cannot write chinese characters to a filename This question is similar to that but a slight change, causes the solution in that question to no longer work.

I am now trying to take in a filename as a program argument and if the characters are Chinese (or Japanese or similar) they will be swapped to ? and I will get an exception.

Run Configurations > Common > Encoding = UTF-8

Program Arguments: 你好.txt

Main:

public static void main(String[] args) throws IOException
{
    String fileName = args[0];

    Writer out = new BufferedWriter(new OutputStreamWriter(
            new FileOutputStream("C:/temp/"+fileName+".txt"), "UTF-8"));//Ex thrown
    out.close();
}

Exception: Exception in thread "main" java.io.FileNotFoundException: C:\temp\??.txt.txt (The filename, directory name, or volume label syntax is incorrect)

How can I make this work?

Community
  • 1
  • 1
Aequitas
  • 2,205
  • 1
  • 25
  • 51
  • The problem is likely with the console you are inputting characters from rather than the program itself. Windows' command prompt for example does not use Unicode (thus any input that is not valid ASCII would get converted to question marks **prior** to being fed into the program). A trivial way to check this is to write a program that merely pipes `args[0]` to a UTF-8 encoded file and examine the results. – initramfs Sep 16 '15 at 04:10
  • @makata same thing but the exception doesn't have the extra `.txt` – Aequitas Sep 16 '15 at 04:19
  • @CPUTerminator I'm using the eclipse "Program Arguments" for testing/debugging but in reality I'd be using sockets to start the application with the filename argument – Aequitas Sep 16 '15 at 04:20
  • You can still perform the same test (by writing a program that pipes the arguments out to a UTF-8 file) to check if Eclipse is corrupting your arguments before they are passed into the program. Worst-case scenario, you hard-code some expected values into the code itself, replacing them with actual implementation when that part is complete. Java is perfectly equipped to handle Unicode strings so the issue you have is strictly implementation-defined (e.g. OS handling of non-unicode characters). – initramfs Sep 16 '15 at 04:25
  • I agree with CPU Terminator . @Aequitas, check that `String fileName ="你好.txt";` works! – makata Sep 16 '15 at 04:32
  • @CPUTerminator what do you mean by `pipe` the arguments? – Aequitas Sep 16 '15 at 04:34
  • Use your existing code, write to a fixed filename (something like `"C:/temp/Test.txt"`) and in the contents of the actual file (written using the `Writer` object you created), you print the contents of the string array `args`. Opening the file up, if you see question marks instead of actual input arguments, Eclipse is corrupting arguments before they reach your program. Apologies for the confusing terminology, I'm using "pipe" informally here, merely meaning to transfer the arguments from input to file. – initramfs Sep 16 '15 at 04:38
  • @makata that gave me an error when I tried to save it saying to save the file as a UTF-8 file which I did and then it worked – Aequitas Sep 16 '15 at 04:39
  • @CPUTerminator I believe I've done what you suggested. `??.txt` is printed into the file when I do that. – Aequitas Sep 16 '15 at 04:41
  • Then that pretty much confirms it, Eclipse is corrupting your arguments **before** it even gets to your program. Bottom line is that your program _should_ work when receiving proper parameters from Unicode-compliant sources. As mentioned, I'd recommend to hard-code the input arguments for now, replacing them later as necessary when input can be properly fed into the program. – initramfs Sep 16 '15 at 04:43

0 Answers0