Question

I wrote up this class based on some examples I found online for Data Streams and I'm getting an EOFException at the end of each run. When I looked it up it said the end of the stream had been reached unexpectedly. It does appear to work on the console (at least it spits out the correct sequence of numbers). The text file is just gibberish though when I inspect the contents after running.

public class DataStreamExample {

    public static void main(String args[]) {
        int choice = 0;
        int[] numbers = { 25, 4, 19, 70, 12, 28, 39, 30 };

        System.out.println("This program will provide an example on how the Data Stream works.");
        System.out.println("Note: Data Streams only work with primative data types.");
        System.out.println();
        System.out.println();


        try {
            System.out.println("1. For write Data operation");
            System.out.println("2. For read Data operation");  
            System.out.println("Enter the number for the operation of your choice: ");

            BufferedReader buffRead = new BufferedReader(new InputStreamReader(System.in));
            choice = Integer.parseInt(buffRead.readLine());
            switch(choice){
                case 1:  
                    FileOutputStream fileOut = new FileOutputStream("Example.txt");
                    BufferedOutputStream buffOut = new BufferedOutputStream(fileOut);
                    DataOutputStream dOutput =new DataOutputStream (buffOut);
                    for (int i = 0; i < numbers.length; i ++) {
                        dOutput.writeInt(numbers[i]);
                    }
                    System.out.print("writing data ");  
                    dOutput.close();

                case 2:
                    FileInputStream fileIn = new FileInputStream("Example.txt");
                    BufferedInputStream buffIn = new BufferedInputStream(fileIn);
                    DataInputStream dInput = new DataInputStream (buffIn);
                    while (true){
                        System.out.print(dInput.readInt());
                    }

                default:
                    System.out.println("Invalid choice");  
            }
        } 
        catch (Exception e) {
            System.err.println("Error in read/write data to a file: " + e);
        }

    }
}

Does anyone have any advice or observations that can help clean this up so I don't get gibberish in the file and so it doesn't catch the exception? How should I be ending the operation and closing the stream?

Was it helpful?

Solution 2

Some things:

  1. When you enter 1 to "write Data operation", you don't have a break; in your case statement, thus when you try to write the data, the integers gets written into the file, then the second case gets executed and you try to read from it. Not sure if this is your intention. But you can add a break to keep that from happening:

            case 1:  
                FileOutputStream fileOut = new FileOutputStream("Example.txt");
                BufferedOutputStream buffOut = new BufferedOutputStream(fileOut);
                DataOutputStream dOutput =new DataOutputStream (buffOut);
                for (int i = 0; i < numbers.length; i ++) {
                    dOutput.writeInt(numbers[i]);
                }
                System.out.print("writing data ");  
                dOutput.close();
                break;
    
  2. When you try to read from your data file, you loop indefinitely:

                while (true){
                    System.out.print(dInput.readInt());
                }
    

    That means it'll keep attempting to read serialized integers until the end of the file is reached. And when the end of the file has been reached, the stream throws an EOFException`. You can try changing your loop to this to prevent the exception:

                while (dInput.available()>0){
                   System.out.print(dInput.readInt());
                }
                break;
    
  3. When you print the integers, they're going to be all jumbled up.

  4. The text file is gibberish because it's not text. When you use a DataOutputStream the objects (or primitives) that you write into the stream gets processed so that the data is portable.

OTHER TIPS

For your input operation, you are reading forever while(true), so the only way that loop will complete is by throwing EOFException. if you don't want the operation to complete that way, you will have to know how many ints you are reading before you start (and use that to limit the loop).

the reason the file looks like gibberish is because you are writing the values as binary. if you want to read the ints in the file, you would have to write them as text.

also, you should put break; statements at the end of each case.

Well, instead of using

while (true){
    System.out.print(dInput.readInt());
 }

you should use

while(dInput.available()>0){
    System.out.print(dInput.readInt());
}

Because when you use while(true), it's alway read from Stream, include when stream reach EOF (End Of File)
After all, you should close all your stream using close() method

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top