Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to write 2 dimensional array of float numbers to binary file?

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to write 2 dimensional array of float numbers to binary file?

    Hi,

    I'm new to Java and struggle with this issue. I tried popular hint from internet as followed but it
    kept writing in huge number. For example, if I have a 101 by101 array of decimal numbers, and would like
    to save in a BINARY file (.dat). Here is my code. Do you see anything wrong, and any suggestions? Thanks

    public static void main(String[] args) throws IOException{
    ...
    ...
    String strFilePath3 = ("C://Users//Documents//workspace//" + "filename"); 
      try 
      {
    	  FileOutputStream fos3 = new FileOutputStream(strFilePath3); 
    	  DataOutputStream dos3 = new DataOutputStream(fos3);
    		  int n=0; int i;
    	for (int j=0; j<2dimenarray.length; j++){
                                for (i=0; i<2dimenarray.length; i++){
    	        dos3.writeDouble(2dimenarray[i][j]);
                               }
               fos3.close();       
       }     
     catch(FileNotFoundException ex)    
      {    System.out.println("FileNotFoundException : " + ex);  }    
      catch(IOException ioe)     
      {      Sys                  }
    }
    Last edited by helloworld922; June 16th, 2010 at 07:24 PM. Reason: please use [code] tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to write 2 dimensional array of float numbers to binary file?

    but it kept writing in huge number
    Please explain how you saw the "huge number"?
    Did you use a hex editor?
    What do you expect to be in the .dat file?

    To verify the file has good data, write another small program that uses readDouble() and printout what it reads.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to write 2 dimensional array of float numbers to binary file?

    Taking a guess here because I have the same questions as Norm, but by big numbers do you mean it is printing out the entire precision of the doubles (which equates to very long numbers - something like 1.090989789789789)? If this is correct, you can format the number to a given precision using a DecimalFormat object, then print out the string that is the result from the formatting. See the following: How to Format a Double

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to write 2 dimensional array of float numbers to binary file?

    @copeg What I think he's seeing is the binary values of doubles one after the other with no separators for all the numbers he wrote out. writeDouble() is a Data output method that would put out the bytes for a double (is that 8) with no delimiters.

    I asked the OP to write a program that used readDouble() and display the result. I expect he'll see his numbers.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to write 2 dimensional array of float numbers to binary file?

    Quote Originally Posted by Norm View Post
    @copeg What I think he's seeing is the binary values of doubles one after the other with no separators for all the numbers he wrote out. writeDouble() is a Data output method that would put out the bytes for a double (is that 8) with no delimiters.

    I asked the OP to write a program that used readDouble() and display the result. I expect he'll see his numbers.
    ahh, your probably right.

Similar Threads

  1. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM
  2. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM
  3. How to Write and Read Binary Data
    By neo_2010 in forum File Input/Output Tutorials
    Replies: 3
    Last Post: January 4th, 2010, 02:38 PM
  4. How to save statistics of a game in a binary file instead of txt file?
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 19th, 2009, 05:05 AM
  5. How to convert float and double data types to binary?
    By rosh72851 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2008, 10:45 PM