Results 1 to 11 of 11
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    200
    Rep Power
    10

    Default Question about DataOutputStream

    I have created a binary file with DataOutputStream. It contains some binary text.
    I want to append text to the end of this file. How to do it?
    If I was using RandomAccessFile, I could just move the pointer to the end, but here I dont know how to.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,068
    Blog Entries
    3
    Rep Power
    14

    Default

    You can have an underlying fileoutputstream which uses the append constructor, check the java 6 Apis.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default

    Look at the FileOutputStream's constructor. It has a parameter to append output to the end of the file.

  4. #4
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    200
    Rep Power
    10

    Default

    I get exception. I dont see how. First, I print these to a binary file:

    PHP Code:
    		String f = "ttest.txt";
    
    		FileOutputStream sout = new FileOutputStream (f);
    		BufferedOutputStream poo = new BufferedOutputStream (sout);
    		DataOutputStream fout = new DataOutputStream (poo);
    
    		fout.writeInt (10);
    		fout.writeInt (23215);
    		fout.writeChar ('A');
    		fout.writeLong (60000950);
    
    		fout.close ();
    		sout.close ();
    		poo.close ();
    Then I append a string the the end of the file in a new program, which I ran after the program above:
    PHP Code:
    		FileOutputStream sout = new FileOutputStream (f, true);
    		BufferedOutputStream poo = new BufferedOutputStream (sout);
    		DataOutputStream fout = new DataOutputStream (poo);
    
    		fout.writeUTF ("hej"); //This string should be at the end of ttest.txt
    
    		//Time to read all this:
    		
    		FileInputStream fi = new FileInputStream (f);
    		BufferedInputStream foo = new BufferedInputStream (fi);
    		DataInputStream fin = new DataInputStream (foo);
    
    		System.out.println (fin.readInt ());
    		System.out.println (fin.readInt ());
    		System.out.println (fin.readChar ());
    		System.out.println (fin.readLong ());
    		System.out.println (fin.readUTF ()); //This should read the appended text(hej), but I get exception
    		fin.close ();
    		foo.close ();
    		fi.close ();

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default

    I get exception
    Please copy and paste here the full text of the error message.

    Did you close the file before trying to read it?
    Last edited by Norm; 06-21-2011 at 07:46 PM.

  6. #6
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    200
    Rep Power
    10

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default

    Did you close the file before trying to read it?

  8. #8
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    200
    Rep Power
    10

    Default

    What do you mean by close the file? If you mean close the stream, then yes, I have
    fout.close ();
    sout.close ();
    poo.close ();
    btw its a runtime exception.
    Last edited by Pojahn_M; 06-21-2011 at 10:00 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default

    What about after this:
    fout.writeUTF ("hej"); //This string should be at the end of ttest.txt

  10. #10
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    200
    Rep Power
    10

    Default

    shit man, I cant believe I make mistakes like that!!!!
    anyway, thanks. Works now.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default

    As a note, you only need to close the DataStreams. They'll close the streams they are wrapping.

Similar Threads

  1. Dataoutputstream
    By saranyabaskaran in forum New To Java
    Replies: 6
    Last Post: 02-12-2011, 02:26 AM
  2. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  3. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  4. Replies: 0
    Last Post: 06-26-2008, 07:34 PM
  5. File I/O with DataOutputStream
    By Tzaphiel in forum New To Java
    Replies: 0
    Last Post: 12-16-2007, 09:39 PM

Posting Permissions

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