views:

115

answers:

3

There are questions like this, that are about guessing charset/encode of a file. But is there a method in Java to ask the system to tell me before try to guess?

+6  A: 

No, there isn't. That's exactly why those questions were asked.

BalusC
so, if I write a file with the esplicit encoding, than I have to remember, or guess, the encoding when I have to read it again?
s.susini
@s.susini: if you use a "plain text" file, yes. Which is why that's not such a great file format. Say what you want about XML, but its explicit encoding declaration can save you a lot of headaches.
Michael Borgwardt
That's correct. Just be consistent with the encoding. UTF-8 is a good choice.
BalusC
+6  A: 

How is "the system" supposed to know? If the encoding isn't declared in some sort of metadata somewhere, then educated guessing is the only thing that can be done.

Michael Borgwardt
As you wrote, it could be declared in some sort of metadata somewhere... such as the extended attributes on MacOS X. So, one could imagine that other systems use similar mechanisms to keep track of text encoding, and that a Java method would provide a unified way to access these mechanisms. No such luck, though :-/
Damien
+1  A: 

There is no way for the system to know what encoding was used when the file was written. The system would need to try make an educated guess based on the file contents, same as you would do yourself from your application.

You will need to implement this yourself, or use an external library such as the ones described in the original question you referenced.

Grodriguez