• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Read from file problems

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using writeByte() and readByte() instead of writeInt() and readInt()? 600 will not fit in a byte.
 
Lora Tully
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Lora Tully
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I dont understand what you mean about highvalue and lowvalue?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic