my dog learned polymorphism*
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 Android Security Essentials Live Lessons this week in the Android 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: 18546
    
  40

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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Read from file problems
 
Similar Threads
Resize an image while maintaing its aspect ratio..
calculate ImageView dimentions before user interact the UI
compile error
Cewolf - Stacked Vertical Bar Chart Bar Width
Scroll around main JFrame?