Java's default encoding is ASCII
. Yes? (See my edit below)
When a textfile is encoded in UTF-8
? How does a Reader know that he has to use UTF-8
?
The Readers I talk about are:
FileReader
sBufferedReader
s fromSocket
s- A
Scanner
fromSystem.in
- ...
EDIT
It turns our the encoding is depends on the OS, which means that the following is not true on every OS:
'a'== 97
You normally specify that yourself in an
InputStreamReader
. It has a constructor taking the character encoding. E.g.All other readers (as far as I know) uses the platform default character encoding, which may indeed not per-se be the correct encoding (such as -cough-
CP-1252
).You can in theory also detect the character encoding automatically based on the byte order mark. This distinguishes the several unicode encodings from other encodings. Java SE unfortunately doesn't have any API for this, but you can homebrew one which can be used to replace
InputStreamReader
as in the example here above:Edit as a reply on your edit:
No, this is not true. The
ASCII
encoding (which contains 128 characters,0x00
until with0x7F
) is the basis of all other character encodings. Only the characters outside theASCII
charset may risk to be displayed differently in another encoding. TheISO-8859
encodings covers the characters in theASCII
range with the same codepoints. TheUnicode
encodings covers the characters in theISO-8859-1
range with the same codepoints.You may find each of those blogs an interesting read:
For most reader, Java uses whatever encoding & character set your platform does -- this may be some flavor of ASCII or UTF-8, or something more exotic like JIS (in Japan). Characters in this set are then converted to the UTF-16 which Java uses internally.
There's a work-around if the platform encoding is different than a file encoding (my problem -- UTF-8 files are standard, but my platform uses Windows-1252 encoding). Create an InputStreamReader instance that uses the constructor specifying encoding.
Edit: do this like so:
However, IIRC there are some provisions to autodetect common encodings (such as UTF-8 and UTF-16). UTF-16 can be detected by the Byte Order Mark at the beginning. UTF-8 follows certain rules too, but generally the difference b/w your platform encoding and UTF-8 isn't going to matter unless you're using international characters in place of Latin ones.
I'd like to approach this part first:
There are at least 4 different things in the Java environment that can arguably be called "default encoding":
byte[]
toString
) at Runtime, when nothing else is specified. This one depends on the platform, settings, command line arguments, ... and is usually just the platform default encoding.char
values andString
objects. This one is always UTF-16! There is no way to change it, it just is UTF-16! This means that achar
representinga
always has the numeric value 97 and a char representingπ
always has the numeric value 960..class
files. This one is always UTF-8. There is no way to change it..java
files. This one defaults to the default charset, but can be configured at compile time.It doesn't. If you have some plain text file, then you must know the encoding to read it correctly. If you're lucky you can guess (for example, you can try the platform default encoding), but that's an error-prone process and in many cases you wouldn't even have a way to realize that you guessed wrong. This is not specific to Java. It's true for all systems.
Some formats such as XML and all XML-based formats were designed with this restriction in mind and include a way to specify the encoding in the data, so that guessing is no longer necessary.
Read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) for the details.
You can start getting the idea here java Charset API
Note that according to the doc,
EDIT :
sorry I got called away before I could finish this, maybe I shouldn't have posted the partial answer as it was. Anyway, the other answers explain the details, the point being that the native file charset for each platform together with common alternate charsets will be read correctly by java.
Java's default encoding depends on your OS. For Windows, it's normally "windows-1252", for Unix it's typically "ISO-8859-1" or "UTF-8".
A reader knows the correct encoding because you tell it the correct encoding. Unfortunately, not all readers let you do this (for example,
FileReader
doesn't), so often you have to use anInputStreamReader
.