Results 1 to 10 of 10
  1. #1
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default [SOLVED] write&read a binary file

    I try to write an int array to a binary file and read that binary file.
    Could anyone give a sample for writing and reading a binary file please:)
    A stitch in time saves nine:D

  2. #2
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    <CODE>
    import java.io.DataOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;

    /**
    * Write some data in binary.
    *
    * @author Ian F. Darwin, Ian Darwin's Site
    * @version $Id: WriteBinary.java,v 1.3 2004/02/08 23:57:29 ian Exp $
    */
    public class WriteBinary {
    public static void main(String[] argv) throws IOException {
    int i = 42;
    double d = Math.PI;
    String FILENAME = "binary.dat";
    DataOutputStream os = new DataOutputStream(new FileOutputStream(
    FILENAME));
    os.writeInt(i);
    os.writeDouble(d);
    os.close();
    System.out.println("Wrote " + i + ", " + d + " to file " + FILENAME);
    }
    </CODE>

    U can start with that to write in, think I have another for reading.. hang on.. 2 secs

  3. #3
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    Sorry, this ones better.


    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;

    class DataIODemo {
    public static void main(String args[]) throws IOException {

    FileOutputStream fout = new FileOutputStream("Test.dat");
    DataOutputStream out = new DataOutputStream(fout);

    out.writeDouble(98.6);
    out.writeInt(1000);
    out.writeBoolean(true);

    out.close();

    FileInputStream fin = new FileInputStream("Test.dat");
    DataInputStream in = new DataInputStream(fin);

    double d = in.readDouble();
    int i = in.readInt();
    boolean b = in.readBoolean();

    System.out.println("Here are the values: " + d + " " + i + " " + b);

    in.close();
    }
    }


    All the best ;)

  4. #4
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default

    thank you. I add the code to my program. and there is a small problem. I try to write an integer array(with size 1000) to a binary file. I can not append all into file. It only writes last one:(
    how can I append ?

    kind regards.
    A stitch in time saves nine:D

  5. #5
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    post your code up here and let us see what you've done

  6. #6
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default

    I would like to write a kind of data compression. So here is a part of code...

    Java Code:
    try{
    						    FileInputStream fstream1 = new FileInputStream("c:\\Output\\scr.img");
    						    
    						    DataInputStream in1 = new DataInputStream(fstream1);
    						        BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
    						    String strLine1;
    						   
    						    while ((strLine1 = br1.readLine()) != null)   {
    						     //hello
    						    	if (z == 0) {
    									try {
    										BufferedWriter out = new BufferedWriter(
    												new FileWriter("c:\\Test.dat",
    														true));
    										out.write(linebyline_reserve[0] + "\r\n");
    										out.close();
    									} catch (IOException e1) {
    									}
    								}
    								if (z == 1) {
    									try {
    										BufferedWriter out = new BufferedWriter(new FileWriter("c:\\Test.dat",true));
    										out.write(linebyline_reserve[1] + "\r\n");
    										out.close();
    									} catch (IOException e1) {
    									}
    								}
    						    //hello
    								if (z != 0 && z != 1) {
    									for (int d = 0; d < counter; d++) {
    										if (strLine1.equals(copy_of_linebyline[d])) {
    
    											FileOutputStream fout = new FileOutputStream("c:\\Test.dat");
    											DataOutputStream out = new DataOutputStream(fout);
    											out.writeInt(map[d]);
    										
    											out.writeBoolean(true);
    
    											out.close();
    
    										}
    									}
    								}
    								z++;	
    						    }
    						    //Close the input stream
    						    in1.close();
    						    }catch (Exception e1){//Catch exception if any
    						      System.err.println("Error: " + e1.getMessage());
    						    }
    Here, I have 2 arrays. one is integer and other one is string.It is easy to append 2 of them into a text file but I could not manage to do it with binary file.
    A stitch in time saves nine:D

  7. #7
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default

    I think problem is loop. I mean when I put a loop here
    Java Code:
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    class DataIODemo {
    public static void main(String args[]) throws IOException {
    for(int i=0; i<20; i++){
    FileOutputStream fout = new FileOutputStream("Test.dat");
    DataOutputStream out = new DataOutputStream(fout);
    
    out.writeDouble(98.6);
    out.writeInt(1000);
    out.writeBoolean(true);
    
    out.close();
    }
    }
    }
    In this case, it overwrites. How can I fix it?

    kind regards.
    A stitch in time saves nine:D

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default move the close() method

    Because your closing the DataOutputStreamit in the for loop... take the close() method out of the loop and place it after the loop.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default

    Thank you it works. I would like to ask one more question. Could you tell me why it works when I call BufferedWriter in a loop and it does not work when I call DataOutputStream. I mean I call .close() with BufferedWriter in a loop and it works.But not with DataOutputStream.

    footnot: could you post related links please(binary file IO).


    kind regards.
    A stitch in time saves nine:D

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Actually, you bring up a good point (and I hadn't noticed it). The following:
    Java Code:
    FileOutputStream fout = new FileOutputStream("Test.dat");
    DataOutputStream out = new DataOutputStream(fout);
    .. shouldn't be in the loop. They should before the loop (you don't have to create them everytime the program loops)... so it should look something like:
    Java Code:
    [COLOR="Blue"]FileOutputStream fout = new FileOutputStream("Test.dat");
    DataOutputStream out = new DataOutputStream(fout);[/COLOR]
    for(int i=0; i<20; i++){
       out.writeDouble(98.6);
       out.writeInt(1000);
        out.writeBoolean(true);
    }
    [COLOR="blue"]out.close();[/COLOR]
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Executable java to read and write to a txt file
    By njoymirror in forum New To Java
    Replies: 2
    Last Post: 03-21-2009, 01:19 AM
  2. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  3. Replies: 5
    Last Post: 01-20-2009, 09:08 AM
  4. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 12:11 PM
  5. File read/write problems
    By p900128 in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 12:15 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
  •