Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8366907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:01:46+00:00 2026-06-09T13:01:46+00:00

I am reading a file in Java using this code import java.io.*; public class

  • 0

I am reading a file in Java using this code

import java.io.*;

public class IOReadDataStreams {
    public static void main(String[] args)throws IOException{
        DataInputStream in = null;

        try{
            in =  new DataInputStream(
                    new BufferedInputStream(new FileInputStream("invoicedata")));
            int unit;
            double price;
            String desc;
            while(true){
                unit = in.readInt();
                price = in.readDouble();
                desc = in.readUTF();
                System.out.println(unit+" " + price +" "+ desc);
            }
        }catch(EOFException e){
            e.printStackTrace();
        }
        finally{
            if(in != null)
                in.close();
        }
    }
}

The file that I was trying to read was made by this Piece of code

import java.io.IOException;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;


public class IODataStreams {
    static final  String  dataFile = "invoicedata";
    static final double[] prices = new double[]{19.99, 9.99,15.99,3.99,4.99};
    static final int[] unitCount = new int[]{12,8,13,29,50};
    static final String[] desc = new String[]{
        "Java T Shirt",
        "C# T Shirt",
        "PHP T Shirt",
        "Ruby T Shirt",
        "Go! T Shirt"
    };

    public static void main(String[] args) throws IOException{
        DataOutputStream out = null;
        DataInputStream in = null;  
        try{
            out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("invoicedata")));
            for(int i = 0 ; i < prices.length; i++){
                out.writeUTF(desc[i]);
                out.writeInt(unitCount[i]);
                out.writeDouble(prices[i]);
            }
        }finally{
            if(out != null)
                out.close();
        }

    }

}

Now I am wondering why my first code (the one that reads the file) is not Printing the price,desc and the unit. it keeps on returning a EOFException

The error is specifically on this line

desc = in.readUTF();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:01:48+00:00Added an answer on June 9, 2026 at 1:01 pm

    You are writing data in this order:

                out.writeUTF(desc[i]);
                out.writeInt(unitCount[i]);
                out.writeDouble(prices[i]);
    

    But reading data in the wrong order:

                unit = in.readInt();
                price = in.readDouble();
                desc = in.readUTF();
    

    You need to read and write the data in the same order, specifically the UTF bit, because that doesn’t translate into a number.

    When I switched these lines so that the read and write was in the same order, this is what IOReadDataStreams outputed:

    12 19.99 Java T Shirt
    8 9.99 C# T Shirt
    13 15.99 PHP T Shirt
    29 3.99 Ruby T Shirt
    50 4.99 Go! T Shirt
    

    Before reaching the EOF of the file and throwing (like it should) an EOFException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following Java code: import java.io.*; class Global{ public static int a =
I have a file in .gz format. The java class for reading this file
I am reading a .wav audio file using Java AudioInputStream.The audio file is 16
I am reading a big file in a Java program, using http access. I
I am reading a ZIP file using java as below: Enumeration<? extends ZipEntry> zes=zip.entries();
i just create reading file feature. In general, what kind PHP class that usually
I'm reading a file using bufferedreader, so lets say i have line = br.readLine();
I am reading a file byte-by-byte. Say for example i have this byte: 0x41
I am reading .csv file through PHP using fgetcsv(). Now I am able to
I am reading one file using ruby in which I have written regexp in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.