2

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.

5
  • What are the encoding settings in the Eclipse project properties ? Commented Jun 8, 2012 at 8:36
  • 1
    Is the file name hard coded in your code or do you read it from somewhere? Commented 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. Commented Jun 8, 2012 at 8:52
  • @SoboLAN I tried default Cp1252 and UTF-8 and it didn't solve my problem. Commented Jun 8, 2012 at 8:53
  • Did you start java with the proprty -Dfile.encoding=whatever ? Commented Jun 8, 2012 at 9:38

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.