Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Trying to Read From A File In Java

Options
  • 28-02-2012 10:19am
    #1
    Closed Accounts Posts: 95 ✭✭


    Hello, I'm trying to read from a file in Java but its giving me huge problems. Now with the system.out.println its telling me that there is an error because the system packet doesn't exist.
    import java.io.*;
    
    public class ClockAssignment {
    
        public static void main (String[] args) {
    
    int sec, min, hour, aSec, aMin, aHour;    
    	
    	try{
    
    		FileInputStream filestream = new FileInputStream ("clock.dat");
    		
    		ObjectInputStream os = new ObjectInputStream (filestream);
    		
    		Object one = os.readObject();
    		Object two = os.readObject();
    		Object three = os.readObject();
    		Object four = os.readObject();
    		Object five = os.readObject();
    		Object six = os.readObject();
    
    		int sec = (int)one;
    		int min = (int)two;
    		int hour = (int)three;
    		int aSec = (int)four;
    		int aMin = (int)five;
    		int aHour = (int)six;
    	
    os.close();
    	} catch(Exception ex) {
    	ex.printStackTrace();
    	}
    	system.out.println (sec + min + hour + aSec + aMin + aHour);
        }
    
    }
    


Comments

  • Registered Users Posts: 880 ✭✭✭moycullen14


    The Crab wrote: »
    Hello, I'm trying to read from a file in Java but its giving me huge problems. Now with the system.out.println its telling me that there is an error because the system packet doesn't exist.
    import java.io.*;
    
    public class ClockAssignment {
    
        public static void main (String[] args) {
    
    int sec, min, hour, aSec, aMin, aHour;    
    	
    	try{
    
    		FileInputStream filestream = new FileInputStream ("clock.dat");
    		
    		ObjectInputStream os = new ObjectInputStream (filestream);
    		
    		Object one = os.readObject();
    		Object two = os.readObject();
    		Object three = os.readObject();
    		Object four = os.readObject();
    		Object five = os.readObject();
    		Object six = os.readObject();
    
    		int sec = (int)one;
    		int min = (int)two;
    		int hour = (int)three;
    		int aSec = (int)four;
    		int aMin = (int)five;
    		int aHour = (int)six;
    	
    os.close();
    	} catch(Exception ex) {
    	ex.printStackTrace();
    	}
    	system.out.println (sec + min + hour + aSec + aMin + aHour);
        }
    
    }
    

    A few problems here:

    Your problem with sec, min, etc is that they're declared twice. First in main() and second within the try { }. Change int sec = (int)one; to sec = (int)one will fix that

    Next, it should be System.out.println, not system.out.println

    Now for the big problem.

    You are assuming that your file consists of 6 integers, so read 6 integers. reading an object is no good, you have to tell it the type. You can do either:

    Integer intObj = os.readInt();

    or

    Integer intObj = (Integer)os.readObject();

    and then

    int prim = intObj.intValue(); // get the int primitive value of Integer Object

    You must understand those two statements. Integer is an Object, int is a primitive. That's a very important distinction.

    What you are doing is reading a file of Objects, not primitives, that was created by java - serialized in other words. Really you should start with the code that writes the file and the reading is then the reverse operation.

    Hope this helps.

    Other things to think about are what if the file does not contain 6 Integer objects?

    Wouldn't it be better is the file contained one object that itself contained 6 Integer objects? That way the read check would only have to be done once.

    Stick with it, it'll come....


  • Closed Accounts Posts: 95 ✭✭The Crab


    Thanks, I've done all that.

    They told us to keep it as six integers as the file only contains six. I understand your point and agree with you but unfortunately their rules are otherwise.

    Now it gives me a problem at runtime - I attached the screen because I really don't understand what's going on.


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    Where did clock.dat come from? Who created it? Can you verify that the data in the file is correct. Java is throwing the exception as data in the file does not conform to its consistency checks.


  • Closed Accounts Posts: 95 ✭✭The Crab


    lynchie wrote: »
    Where did clock.dat come from? Who created it? Can you verify that the data in the file is correct. Java is throwing the exception as data in the file does not conform to its consistency checks.

    The college created it. I saved it into the same folder. It does look weird though as the file seems to have the icon of a windows media player file. When I opened it in its zipped format it showed six number.

    Just checked and it actually describes itself as a Video CD Movie. Opening it with Notepad++ does however still show the six numbers.


  • Registered Users Posts: 880 ✭✭✭moycullen14


    As we have said, you really need to know HOW the file was created.

    An ObjectInputStream can only be used to read a stream created by ObjectOutputStream - which puts a header in the file to tell whats in it, what Objects. What you perhaps need to use is DataInputStream, for example.


    DataInputStream dis = new DataInputStream(new FileInputStream ("clock.dat"));
    int sec = dis.readInt();
    etc, etc

    Have a look at http://www.kodejava.org/examples/214.html, it shows how to use DataInput/OutputStreams together. The important thing is you use the SAME code to read as was used to write.

    ObjectInput/OutputStream is used for complex objects that can be written/read in one operation. DataInput/OutputStream is used for primitive (i.e. int, float, char) datatypes. I'd say if you change your code to use DIS it'll work fine.


  • Advertisement
  • Closed Accounts Posts: 95 ✭✭The Crab


    Thanks, I tried to follow something along those lines but it still tells me java.io.EOFException

    at java.io.DataInputStream.readInt (DataInputStream.java:392)
    at ClockAssignment.main(ClockAssignment.java:25)
    2038387793

    Please help. I've tried contacting my lecturer in every which way and he seems to have no interest in getting back to me.
    import java.io.*;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class ClockAssignment {
    
        public static void main (String[] args) {
        
    	int sec = 0; 
    	int min = 0; .
    	int hour = 0; 
    	int aSec = 0;
    	int aMin = 0; 
    	int aHour = 0;;
    	
    	try{
    
    		DataInputStream dis = new DataInputStream (new FileInputStream ("clock.dat"));
    		
    		sec = dis.readInt();
    		min = dis.readInt();
    		hour = dis.readInt();
    		aSec = dis.readInt();
    		aMin = dis.readInt();
    		aHour = dis.readInt();
    	
    dis.close();
    	} catch(Exception ex) {
    	ex.printStackTrace();
    	}
    	System.out.println (sec + min + hour + aSec + aMin + aHour);
        }
    
    }
    


  • Registered Users Posts: 880 ✭✭✭moycullen14


    What's happening is you're reading beyond the end of the file. You're trying to read 6 integers (say 24 bytes) and the file isn't that big. Maybe the sec, hour and minute are stored as Bytes? (hour, min & sec)

    How big is the file?

    Can you put it online somewhere?

    Can you provide a hex dump(http://www.richpasco.org/utilities/hexdump.html) of the file?

    Put the following lines in your program:

    System.out.println("sec = " + sec); // and so on....

    After each readInt and see what happens


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    The Crab wrote: »
    Thanks, I tried to follow something along those lines but it still tells me java.io.EOFException

    at java.io.DataInputStream.readInt (DataInputStream.java:392)
    at ClockAssignment.main(ClockAssignment.java:25)
    2038387793

    Please help. I've tried contacting my lecturer in every which way and he seems to have no interest in getting back to me.

    http://docs.oracle.com/javase/6/docs/api/java/io/DataInputStream.html#readInt()
    readInt

    public final int readInt()
    throws IOException
    See the general contract of the readInt method of DataInput.
    Bytes for this operation are read from the contained input stream.

    Specified by:
    readInt in interface DataInput
    Returns:
    the next four bytes of this input stream, interpreted as an int.
    Throws:
    EOFException - if this input stream reaches the end before reading four bytes.
    IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.
    See Also:
    FilterInputStream.in


  • Closed Accounts Posts: 95 ✭✭The Crab


    I changed it to bytes. Now there is no runtime error but it produces 142 instead of 0 0 0 5 23 15 ???

    When I tried to read just the first int it returns 806160944.

    I'm so frustrated. AAARRRGGGHHHHH :(.


  • Registered Users Posts: 880 ✭✭✭moycullen14


    The Crab wrote: »
    I changed it to bytes. Now there is no runtime error but it produces 142 instead of 0 0 0 5 23 15 ???

    When I tried to read just the first int it returns 806160944.

    I'm so frustrated. AAARRRGGGHHHHH :(.

    Answer the questions I posed above


  • Advertisement
  • Registered Users Posts: 1,686 ✭✭✭RealistSpy


    I have a java Io library if you want to try that. Might make things easier for you.

    /portfolio/java-projects/java-library


  • Closed Accounts Posts: 95 ✭✭The Crab


    Thanks to everyone. I used a lot of your advice and finally found a way around it with java scanner. I am completely useless at this stuff so having help on here is great :)!


Advertisement