Results 1 to 4 of 4
  1. #1
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default Reading .bin files

    So I have the following code:

    Java Code:
    DataOutputStreamout = new DataOutputStream(newFileOutputStream("C:/Users/******/Desktop/data.bin"));
    		out.writeDouble(3.14159);
    		out.writeInt(500);
    		out.writeBoolean(false);
    		out.close();
    		
    		
    		}
    Now, I have to write code to read those files back into variables and print them back out again. So for example, I have to have a double variable that reads in the 3.14159, an int variable that reads in the 500, and so on. How do I read .bin files in Java to be able to do this?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    By turning it around:
    Java Code:
    DataInputStream in = new DataInputStream(new FileInputStream("C:/Users/******/Desktop/data.bin"));
    double d = in.readDouble();
    int i = in.readInt();
    boolean b = in.readBoolean();
    in.close();
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    I don't know what is in a .bin file.
    If you can give me the format, byte by byte of what is in the .bin file, I will try to show you how to read it.
    For the file you wrote to a DataOutputStream, look at the DataInputStream class for the methods to read the data back. Obviously they must match exactly what was written out.

  4. #4
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    By turning it around:
    Java Code:
    DataInputStream in = new DataInputStream(new FileInputStream("C:/Users/******/Desktop/data.bin"));
    double d = in.readDouble();
    int i = in.readInt();
    boolean b = in.readBoolean();
    in.close();
    Yeah hey that works, thanks man. I guess what you did makes sense, I didn't know about the DataInputStream class until now, lol

Similar Threads

  1. Reading in Text Files
    By blueduiker in forum New To Java
    Replies: 9
    Last Post: 05-17-2010, 05:45 AM
  2. Reading .txt files
    By cvcs1 in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 09:07 AM
  3. reading Excel Files
    By wdb07159 in forum New To Java
    Replies: 2
    Last Post: 05-06-2009, 12:45 PM
  4. reading files and arraylists
    By infinity in forum Advanced Java
    Replies: 2
    Last Post: 01-09-2009, 04:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •