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 4 of 4

Thread: How to Write and Read Binary Data

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Smile How to Write and Read Binary Data

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
     
    public class TEST {
     
        public static void main(String args[]) throws FileNotFoundException, IOException {
            TEST t = new TEST();
            t.Read();
        }
     
        public void Write() throws FileNotFoundException, IOException {
            DataOutputStream dos = new DataOutputStream(new FileOutputStream("Data.bin"));
     
            double price[] = {950, 450, 6};
            int units[] = {3, 6, 9};
            String itemDisc[] = {"IPOD", "CD Player", "HeadPhone"};
     
            for (int x = 0; x < 3; x++) {
                //Write Price
                dos.writeDouble(price[x]);
                System.out.println(price[x] + " Has been written...");
                dos.writeChar('\t');
                System.out.println('\t' + " Has been written...");
     
                //Write Units
                dos.writeInt(units[x]);
                System.out.println(units[x] + " Has been written...");
                dos.writeChar('\t');
                System.out.println('\t' + " Has been written...");
     
                //Write Item Description
                dos.writeChars(itemDisc[x]);
                System.out.println(itemDisc[x] + " Has been written...");
                dos.writeChar('\t');
                System.out.println('\t' + " Has been written...");
            }
     
            dos.close();
        }
     
        public void Read() throws FileNotFoundException, IOException {
            DataInputStream dis = new DataInputStream(new FileInputStream("Data.bin"));
            double price;
            int unit;
            StringBuffer item;
            double totalInv = 0;
     
            char lineSep = '\t';
     
            try {
                while (true)
                {
                    price = dis.readDouble();
                    dis.readChar();//Ignore tab character...
     
                    unit = dis.readInt();
                    dis.readChar();//Ignore tab character...
     
                    char chr;
                    item = new StringBuffer(20);
                    //Read item
                    while ((chr = dis.readChar()) != lineSep) {
                        item.append(chr);
                    }
     
                    System.out.println("You've ordered " +
                            unit + " units of " +
                            item.toString() + " at $" + price);
                    totalInv += (unit * price);
                }
     
            } catch (IOException ex) {
            } finally {
                if (totalInv > 0) {
                    System.out.println("Total Invoice amount is :" + totalInv);
                }
                dis.close();
            }
     
        }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Write and Read Binary Data

    i'm slightly confused... is this a tip, or is there something you need help with?

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Default Re: Write and Read Binary Data

    Thanks a lot helloworld922,
    It just a tip to view how to interact with binary data and files...

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Write and Read Binary Data

    I've moved this to the correct forum now. Thanks neo
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Writing in a file using Java
    By JavaPF in forum File Input/Output Tutorials
    Replies: 4
    Last Post: December 17th, 2011, 04:33 PM
  2. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 11:47 AM
  3. How to write above a JPanel
    By bruno88 in forum AWT / Java Swing
    Replies: 4
    Last Post: June 23rd, 2009, 07:16 PM
  4. Someone please help me write a simple program!!!
    By ocean123 in forum Loops & Control Statements
    Replies: 3
    Last Post: June 14th, 2009, 10:46 PM
  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, 11:45 PM