I have String in java which is filename containing umlauts. File is stored on Win 7 Pro disk correctly (umlauts etc. are shown correctly in explorer file listing). I also tried to save filename to text file and then filename was correctly outputted with umlauts. But when I use method exists() from File, it says file doesn't exists. If I try to use method createNewFile(), it creates file like ä.txt (originally ä.txt). What could be wrong in my settings here? I'm using Tomcat 6 and Eclipse to run my web application.
-
What are the encoding settings in the Eclipse project properties ?Radu Murzea– Radu Murzea2012-06-08 08:36:41 +00:00Commented Jun 8, 2012 at 8:36
-
1Is the file name hard coded in your code or do you read it from somewhere?Robert– Robert2012-06-08 08:44:18 +00:00Commented Jun 8, 2012 at 8:44
-
@Robert It's read from http request, but if i write that string to text file, then it is correctly printed, so it cannot be related to that.newbie– newbie2012-06-08 08:52:37 +00:00Commented Jun 8, 2012 at 8:52
-
@SoboLAN I tried default Cp1252 and UTF-8 and it didn't solve my problem.newbie– newbie2012-06-08 08:53:27 +00:00Commented Jun 8, 2012 at 8:53
-
Did you start java with the proprty -Dfile.encoding=whatever ?Bruno Grieder– Bruno Grieder2012-06-08 09:38:58 +00:00Commented Jun 8, 2012 at 9:38
1 Answer
If the file name would be included as static constant in your source code it would not make a difference where your code is being executed, but as you are reading the filename from an remote address it makes a significant difference.
By default every Java instance as a default charset on Windows this is usually "Cp1252", on other systems usually "UTF-8". Therefore every method that is reading or writing Strings from/to network or file system the default charset is used - as long as you don't use the method versions where the charset is explicitly specified.
Therefore writing the file-name into a file doesn't demonstrates everything because if it is displayed correctly depends on the text editor you are using not on the Java program writing it.
Conclusion: Go through your code and make sure you explicitly set the charset. This is especially relevant for methods getBytes() of String and every where you have a Reader/Writer instance connected to an InputStream/OutpuStream.