Re: Some questions about the IO package
I'm going to continuing posting, even though I'm obviously not the
expert here, just because I don't mind being wrong. It's how I learn
stuff. :)
jtl.zheng wrote:
do you means I should tell BufferedRead to use which encodes?
what I guest is that the JVM will decide which encodes to use
automaticly.
is it right?
but I don't know where do the JVM know what encodes should be used,
it judge it from where? the head of a file?
I think determining file encoding is a system level responsibility; Java
just follows what the system provides.
Java has a default Reader/Writer that provides the defaults for your
system. I think these are InputStreamReader and OutputStreamWriter.
try {
InputStreamReader convert = new InputStreamReader(System.in);
BufferedReader in = newBufferReader(convert);
String text = in.readLine();
int i = NumberFormat.getInstance().parse(text).intValue();
}
catch ( IOException ) {}
catch { ParseException ) {}
The above is correct according to O'Reilly's Learning Java. (It may be
a bit old, however.) You should make sure that InputStreamReader and
OutputStreamWriter are the correct classes for your locale.
However, what Chris was talking about was something like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Remember me this weekend</message>
</note>
Ah ha! Now we have a file format that tells us what the encoding is!
Java won't recognize this automatically with Readers and Writers. (It
might with javax.parser.xml however.) You the programmer have to know
to read the encoding string, and then reopen the file (or whatever) with
the correct Reader filter.
XML is more of a "real world" answer. The Reader/Writer answer I gave
first is more of a "Java only" answer. The programmer must know which
to use and when.
what is the difference between "rws" and "rwd"
I don't know, sorry.
what is the "file's content" and "metadata" mean?
I think this refers to a multi-stream or multiple fork file model. The
"file's contents" refers to all the data. The stuff that InputStream
reads or OutputStream writes.
The "metadata" is stuff that IS NOT part of the file. These are things
that one doesn't read with InputStream nor write with OutputStream. For
example, the file name itself is not read with InputStream, the
programmer must supply that from some other source. Ditto with things
like access permission, and file creation and last modified dates and
times. Those aren't part of the file data, they are elsewhere.
Does that make sense?
Thank you very much in advance
: )