Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

File write problem - Mad

Thread view: 
mark@runnersnation.com - 12 Mar 2006 18:42 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.