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

Thread: Why is my array not being read in correctly from my file?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is my array not being read in correctly from my file?

    In one file, I save a TreeNode object, then an int, and then a byte array to a file named `huffmanTrial.huff`.
    In another file I want to read in all three of those things and do work on them. I can read in the TreeNode just fine and the int, but when I get to reading in the byte array I get that it is of size zero. Is that saying that it doesn't exist? I am using `in.available()` after I read in the TreeNode and int so I can see how big I need to make my new byte array and I always get 0. Am I somehow saving the array wrongly or am I reading it in wrongly?

    Here is where I save the objects:

         FileOutputStream fos = new FileOutputStream(savedFileName);
                ObjectOutputStream out = new ObjectOutputStream(fos);
     
                out.writeObject(root);
                out.writeInt(numberOfZeroesAdded);
                out.writeObject(encodedByteArray);
     
                 out.close();

    And here is where I am trying to read all three things in

      FileInputStream fis = new FileInputStream("huffmanTrial.huff");
                ObjectInputStream in = new ObjectInputStream(fis);
                root = (HuffmanNode) in.readObject();
                System.out.println("root "+ root.getCount());
                numberOfZeroesAdded = in.readInt();
                System.out.println("zeroes: "+numberOfZeroesAdded);
                sizeOfArray = in.available();
                System.out.println("Size of array: "+sizeOfArray);
                encodedByteArray = new byte[sizeOfArray];
                in.readFully(encodedByteArray);

    I get `Size of array: 0`
    I know for sure that the array that I am saving is not empty because just two lines before I save it, I print its contents. What am I doing wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why is my array not being read in correctly from my file?

    The code writes the array with writeObject() instead of write(). Did you try using write()?

    Can you make a small simple program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is my array not being read in correctly from my file?

    Wow, yeah that totally did it. Thanks! I'm glad it was that simple. I guess I was thinking of my array, like an arrayList, as being an object.

Similar Threads

  1. How to read a String .dat file into an array
    By Wallatrix in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 5th, 2013, 02:55 PM
  2. How to read a 2 dimensional text file from an array?
    By seaofFire in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 9th, 2012, 07:44 AM
  3. Read txt file into array and create new output.
    By Margaret_Girl87 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 19th, 2012, 12:08 PM
  4. Read in file and store in 2D array start of The Game of Life
    By shipwills in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2011, 09:52 AM
  5. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 09:13 PM

Tags for this Thread