views:

77

answers:

1

I'm studying Java on my own. One of the exercises is the following, however I do not really understand what it is asking to.... any smart java gurus out there that could explain this in more detail and simple words? Thanks

Suppose that you have a binary file that contains numbers whos type is either int or double. You dont know the order of the numbers in the file, but their order is recorded in a string at the begining of the file. The string is composed of the letters i for int, and d for double, in the order of the types of the subsequent numbers. The string is written using the method writeUTF.

For example the string "iddiiddd" indicated that the file contains eight values, as follows: one integer, followed by two doubles, followed by two integers, followed by three doubles.

Read this binary file and create a new text file of the values written one to a line.

+3  A: 

writeUTF is a method on DataOutputStream, and you can read in the corresponding data using the readUTF method on DataInputStream. Therefore the steps to reading the binary file are:

  • Open a new DataInputStream
  • Read in the string using readUTF
  • Loop through the characters of the string one at a time.
  • If the character is an 'i', use readInt() to read in an int.
  • If the character is a 'd', use readDouble() to read in the double.
  • Close the DataInputStream.

You will also have to write these out to a text file, for which you can use a FileWriter.

Simon Nickerson
Thanks.............