Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems Adapting code

 
Lora Tully
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote code last week for a program I am working on in my spare time. In the code I use an In file and OUT file. I now want to adapt it to instead read the IN file and output the data into an int array. COuld this be done as I think it would be hard to keep the order of byte in the write order etc...

while (i == 1)
if (first_byte == second_byte)
{// Decompression
first_byte = IN.readByte ();// Get repeat count
compressCount ++;

// Set up loop to print first_byte + 2 of second_byte
for (count = -1; count <= first_byte; count ++)
{
OUT.write (second_byte);
byteCount += 2 + first_byte;// increment output chars
first_byte = IN.readByte ();// get new first byte
compressCount ++;
} else{ // Not compressed
OUT.write (first_byte);
byteCount ++;
first_byte = second_byte; // Shift up chars
}
second_byte = IN.readByte (); // Read another
compressCount ++;
}
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can certainly write to a String and read from the String with StringWriter and StringReader, which support the usual I/O methods.
To put information into an int[] array and get the info back is easy to do, but not using the I/O methods.

You could look into the DataInputStream/DataOutputStream methods like readByte() and writeInt() which do I/O with primitives in a platform independent format.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic