CharSet question ?

  • Thread starter =?ISO-8859-1?Q?Mart_K=E4sper?=
  • Start date
?

=?ISO-8859-1?Q?Mart_K=E4sper?=

Hi, all

suppose I have an implementation that reads raw files with data using
BufferedReader(FileReader).read(char[],int,int) invocations. This
returns an array of char's that each may be in the range 0..255.

This worked perfectly OK in JDK 1.1 but not using 1.4 ... what I'm
actually using it for is to later on cast each char to a short, thereby
expecting a value 0..255.

However, when reading a char with the value of e.g. 211, the casted-to
short gets -3 which confuses the application to some extent.

If I still wish to use BufferedReader (I use mark() and ready()), how
can I continue to read the integer values from a file ?

Thanks in advance :)
 
?

=?ISO-8859-1?Q?Mart_K=E4sper?=

Hi, again

am I right in assuming that the line:

br = new BufferedReader(
new InputStreamReader( new FileInputStream(
currentFile), "ISO-8859-1"), 16384);

instead of:

br = new BufferedReader( new FileReader(currentFile), 16384);

where br is a BufferedReader and currentFile is a File object, will
solve the problem ? It appears to have done so for me, but I'm not sure
it's the best way to do it ...

Anyway, thanks for listening ;-)
 
T

Thomas Weidenfeller

Mart Käsper said:
suppose I have an implementation that reads raw files with data using
BufferedReader(FileReader).read(char[],int,int) invocations. This
returns an array of char's that each may be in the range 0..255.

Readers are not intended for reading raw data. Use an InputStream
instead.

/Thomas
 
G

Gordon Beaton

suppose I have an implementation that reads raw files with data using
BufferedReader(FileReader).read(char[],int,int) invocations. This
returns an array of char's that each may be in the range 0..255.

This worked perfectly OK in JDK 1.1 but not using 1.4 ... what I'm
actually using it for is to later on cast each char to a short, thereby
expecting a value 0..255.

However, when reading a char with the value of e.g. 211, the casted-to
short gets -3 which confuses the application to some extent.

BufferedReader (in fact none of the Reader classes) is not an
appropriate class for reading the file the way you've described. If
you want to read binary data you should be using an InputStream.

The Reader attempts to interpret each byte (or combination of bytes,
depending on the character endcoding you've specified) as a character.
That mapping may or may not be what you expect.
If I still wish to use BufferedReader (I use mark() and ready()),
how can I continue to read the integer values from a file ?

Why is it important to use BufferedReader for this? InputStreams have
equivalent methods to mark() and ready().

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top