how to write a double to a file in java

Discussion in 'Java' started by Gaurav, Oct 10, 2003.

  1. Gaurav

    Gaurav Guest

    Hello,

    I am trying to figure out how to write double values to a file in
    java.

    I tried using RandomAccessFile writeDouble but when i look in the file
    i see all junk...

    what is the correct way of writing doubles to a file so i can read
    them .

    thank you,
    Gaurav
     
    Gaurav, Oct 10, 2003
    #1
    1. Advertising

  2. Gaurav

    Chris Smith Guest

    Gaurav wrote:
    > I am trying to figure out how to write double values to a file in
    > java.
    >
    > I tried using RandomAccessFile writeDouble but when i look in the file
    > i see all junk...
    >
    > what is the correct way of writing doubles to a file so i can read
    > them .


    You need to be more precise about what you want. Do you want doubles in
    text format? If so, convert your doubles to String first --
    String.valueOf(myDouble) -- and then write them to a text stream in a
    known text encoding (such as ASCII or UTF-8) with Writer.write.
    Something like:

    FileOutputStream stream = new FileOutputStream("myfile.txt");
    OutputStreamWriter out = new OutputStreamWriter(stream, "US-ASCII");
    out.write(String.valueOf(myDouble));

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Oct 10, 2003
    #2
    1. Advertising

  3. Gaurav

    VisionSet Guest

    "Gaurav" <> wrote in message
    news:...
    > Hello,
    >
    > I am trying to figure out how to write double values to a file in
    > java.
    >
    > I tried using RandomAccessFile writeDouble but when i look in the file
    > i see all junk...
    >
    > what is the correct way of writing doubles to a file so i can read
    > them .
    >


    Works okay for me:

    try{

    RandomAccessFile raf = new RandomAccessFile("RafTest","rw");

    raf.writeDouble(12.34);

    raf.close();

    raf = new RandomAccessFile("RafTest","r");

    double dub = raf.readDouble();

    System.out.println(dub);
    }

    catch(Exception e) {
    e.printStackTrace();
    }


    --
    Mike W

    Sun Certified Programmer for the Java 2 Platform 1.4 :)
    Self congratulation for a limited time only ;-)
     
    VisionSet, Oct 10, 2003
    #3
  4. Gaurav

    Roedy Green Guest

    On 10 Oct 2003 10:53:22 -0700, (Gaurav) wrote
    or quoted :

    >
    >I am trying to figure out how to write double values to a file in
    >java.
    >
    >I tried using RandomAccessFile writeDouble but when i look in the file
    >i see all junk...


    that is what you are supposed to see, unreadable binary.

    See http://mindprod.com/fileio.html


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 10, 2003
    #4
  5. Gaurav

    sheetalmalhotra

    Joined:
    Jan 9, 2008
    Messages:
    1
    writing double values to text file

    Hii everybody...
    I want to create an output file and then write a point of double values into a text file using java.
    eg: (2.00,1.11)

    Is there a way to do that. Please let me know.

    Thanks.
     
    sheetalmalhotra, Jan 9, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Web learner

    from List <double> to double[]

    Web learner, Apr 25, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    484
  2. sb
    Replies:
    4
    Views:
    308
    Alberto Barbati
    Feb 19, 2004
  3. Jacek Dziedzic
    Replies:
    5
    Views:
    385
    Old Wolf
    Apr 8, 2004
  4. ferran
    Replies:
    9
    Views:
    3,039
    Kevin Goodsell
    Apr 12, 2004
  5. Sydex
    Replies:
    12
    Views:
    6,506
    Victor Bazarov
    Feb 17, 2005
Loading...

Share This Page