Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default Dataoutputstream

    hai friends,
    im now working with dataoutputstream object.... i wrote the coding for writing the data into a file using dataoutputstream..bt i culd not see the integers wriiten into my file properly using writeInt method...y is it like dat?

    the code is given below....
    import java.io.DataOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.*;
    public class writeint
    {
    public static void main(String[] args) throws Exception
    {

    try
    {
    File file=new File("d:\\write.txt");
    FileOutputStream fos = new FileOutputStream(file);
    DataOutputStream dos = new DataOutputStream(fos);
    int i = 100;
    dos.writeInt(2333);
    dos.writeChars("hello");
    dos.close();
    }
    catch (IOException e)
    {
    System.out.println("IOException : " + e);
    }
    }
    }

    output file:

     h e l l o

    thanxs...

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Because it is writing the ints as binary which most likely produces undisplayable characters. You might just write the string version of the numbers instead. Remember that text files are ASCII, so writing actual numbers, for example 32 would write a ' ' (space) character to the file, 65 is an A, 66 is a B and so forth.

  3. #3
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    Put a e.printStackTrace(); in your catch statement.

    This might be simply shows the int as characters in the ASCII. Have you tried reading it back in and displaying it? Not all writers are human readable. For human readability, I always use PrintWriter.

  4. #4
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    ya it is printing ascii characters 4 certain integers........ how to overcome dis?

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    And my answer for you on your other post on the same topic wasn't clear enough?
    Dataoutputstream

    Double Post: Dataoutputstream

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Three posts from a duplicate thread have been copied into this thread. Again, I ask that the original poster not repost questions.

  7. #7
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    o...i got it now...its clear ..thanx..but if i need 2 write the user input into a file, wat i need 2 do...especially integers....

    thank u...

Similar Threads

  1. Replies: 0
    Last Post: 06-26-2008, 07:34 PM
  2. 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
  •