hi,
i'm currently writing a simple graphics app which includes a save
file function. This works 99% of the time but when reading the file
back there are anomalies in some pixels (only about 1%) they're saved
as ARGB but that seems irrelevant as a hexeditor shows the file doesn't
seem to contain the bytes i'm sending it.
much head scratching and testing lead to me to notice that only certain
values seem to cause problems - 140 for one, so a bit of testing lead
to this :-
File f = new File("writetest");
FileWriter out = new FileWriter(f);
int i=140;
d("write byte ="+i); // writes to sys out
out.write(i);
i++;
d("write byte ="+i);
out.write(i);
out.close();
FileReader in = new FileReader(f);
i = in.read();
d("read byte = "+i);
i = in.read();
d("read byte = "+i);
in.close();
which gives the follwing output !!!!
write byte =140
write byte =141
read byte = 63
read byte = 63
as i mentioned 99% of pixels are correct (on screen and in hex editor)
have i gone nuts or can anyone shed any light? winxp and jdk 1.4.2 by
the way.
many thanks in advance
mark
Andrey Kuznetsov - 12 Mar 2006 18:58 GMT
> File f = new File("writetest");
> FileWriter out = new FileWriter(f);
[quoted text clipped - 5 lines]
> out.write(i);
> out.close();
FileWriter is definitely wrong thing for writing binary data.
Use FileOutputStream.

Signature
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Casey Hawthorne - 12 Mar 2006 19:57 GMT
Windows distinguishes between binary and text files!
You are using a text output routine which is inserting newline
characters!
>hi,
>
[quoted text clipped - 39 lines]
>
>mark
--
Regards,
Casey
Oliver Wong - 15 Mar 2006 18:58 GMT
[post reordered]
>>File f = new File("writetest");
>>FileWriter out = new FileWriter(f);
[quoted text clipped - 10 lines]
> You are using a text output routine which is inserting newline
> characters!
Actually, Windows doesn't make any particular distinction between these
two types of files. Windows Explorer might distinguish between files based
on their extension, but again this is different from the nature of their
content.
Also, the write() methods does not insert newline characters. You might
be thinking of the println() methods of PrintWriter.
The problem has to do with the fact that the write(int) method
interprets its argument as a character, so in the particular encoding that
the OP is using (perhaps ASCII), 140 maps onto the same character as 141 and
63 (the question mark character). This is because character codes above 127
are not defined in ASCII.
- Oliver
Paul Hamaker - 12 Mar 2006 22:27 GMT
Mark,
writers/readers are for characters, streams are for bytes, binary data.
Here's a lesson about streams :
http://javalessons.com/cgi-bin/fun/java-tutorials-main.cgi?ses=ao789&code=bst&sub=adv
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com
Roedy Green - 13 Mar 2006 00:00 GMT
> i'm currently writing a simple graphics app which includes a save
>file function. This works 99% of the time but when reading the file
>back there are anomalies in some pixels (only about 1%) they're saved
>as ARGB but that seems irrelevant as a hexeditor shows the file doesn't
>seem to contain the bytes i'm sending it.
FileWriters are for preparing human-readable text. You want precise
binary. see http://mindprod.com/applets/fileio.html
for sample read/write code with DataStreams.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
mark@runnersnation.com - 13 Mar 2006 09:47 GMT
Many thanks all, switched to FileInput/OutputStreams and bingo, very
much appreciated everyone who responded.