File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Read from file problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read from file problems" Watch "Read from file problems" New topic
Author

Read from file problems

Lora Tully
Greenhorn

Joined: Nov 29, 2004
Posts: 8
Hi all. I have two classes in my program. Part of one class writes out dimensions of an image to a file, then adds a array after the dimensions...

OUTFILE= new DataOutputStream(new FileOutputStream(PathFileOut));
OUTFILE.writeByte(width);
OUTFILE.writeByte(height);



Now I was wondering how to get that back. I used...

IN = new DataInputStream (new FileInputStream (PATHIN));
width = IN.readByte()



But when displaying that on screen it says 96, rather then 600. Am I doing something wrong? Is it due to the width being stored as more then two bytes or something?
Thanks
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Why are you using writeByte() and readByte() instead of writeInt() and readInt()? 600 will not fit in a byte.


Mike Gershman
SCJP 1.4, SCWCD in process
Lora Tully
Greenhorn

Joined: Nov 29, 2004
Posts: 8
It was part of my task and it had to be a writeByte and readByte. Thats the problem I am having. Is there a way of writing 600 in two bytes then and then reading 2 bytes? If I am remember one byte has 8 bits, so thats 256? so 2bytes would cover 600 but I dont know how to write it

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Originally posted by Lora Tully:
It was part of my task and it had to be a writeByte and readByte. Thats the problem I am having. Is there a way of writing 600 in two bytes then and then reading 2 bytes? If I am remember one byte has 8 bits, so thats 256? so 2bytes would cover 600 but I dont know how to write it


I am a huge fan of schools that teach assembly language first. Students seem to develop a strong understanding and comfort level around bit-operators like &, |, !, >>, and <<... Or maybe I am bias because I spent too many years developing with assembly...


However, you don't have to get fancy. You can just divide by 100 for the high value. And get the remainder for the low value.

highvalue = x / 100;
lowvalue = x % 100;

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Lora Tully
Greenhorn

Joined: Nov 29, 2004
Posts: 8
Sorry I dont understand what you mean about highvalue and lowvalue?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Read from file problems
 
Similar Threads
Cewolf - Stacked Vertical Bar Chart Bar Width
Scroll around main JFrame?
calculate ImageView dimentions before user interact the UI
compile error
Resize an image while maintaing its aspect ratio..