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

reading from a file in java

Options
  • 27-01-2004 10:50pm
    #1
    Registered Users Posts: 819 ✭✭✭


    The following code is part of a bigger application.Compiles and runs fine but whenever i try to open a file with the JFileChooser i get an ioException.Specifically the one on line 49.

    catch (IOException ioException) {
    JOptionPane.showMessageDialog(this, "Error Opening File",
    "Error", JOptionPane.ERROR_MESSAGE);
    }


    Can't see what problem is ...
    Quite possibly something stupid/obvious as i'm fairly new to java.


Comments

  • Registered Users Posts: 3,548 ✭✭✭Draupnir


    youre gonna need to show the code for opening the file for anyone to now why its firing an error, just post up the try block which is linked to that catch


  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    Emm do you mean a HeadlessException? that's what the Java site says it throws.


  • Registered Users Posts: 3,548 ✭✭✭Draupnir


    sorry bout that, i got your code there now, I reckon for a start you should remove the return statement from line 34.

    but i think your main problem is that your FileInputStream should be a FileOutputStream. give that a go


  • Registered Users Posts: 3,548 ✭✭✭Draupnir


    dosent a headless exception actually relate to graphical displays? im pretty sure the problem is the input streams should be output streams, then i reckon it will work, if what you are trying to do is open the files.


  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    How about making fileName a set file just for testing purposes?, hard code it to be a file you know you can get to. Ignore what I sayd above^^ I'm half asleep.


  • Advertisement
  • Registered Users Posts: 3,548 ✭✭✭Draupnir


    pity the chap who had the problem aint around so he could try it out.


  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    Originally posted by Draupnir
    dosent a headless exception actually relate to graphical displays?
    Yep it does:D, damn, you beat me to it!, I'm going to bed before I make any bigger a fool of myself.


  • Registered Users Posts: 819 ✭✭✭sixpack's little hat


    Its definitely supposed to be an ObjectInputStream. The commented code for output was just to see would it open a file when it was for output,which it did.

    System.out.println(fileName);

    does print out the directory/file i select with the JFileChooser but for some reason it doesn't open this file hence the ioException.


  • Registered Users Posts: 819 ✭✭✭sixpack's little hat


    changing the ioException catch to ..

    catch (IOException ioException) {
    JOptionPane.showMessageDialog(this, "Error Opening File"
    + ioException.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }


    the error message says "invalid stream header".

    googled it but didn't find anything that helped.


  • Registered Users Posts: 3,548 ✭✭✭Draupnir


    if you wanna open a file, the output stream does the trick not the input stream, of this i am certain


  • Advertisement
  • Closed Accounts Posts: 30 Lu[ifer


    surely an output stream is used for writing to a file, and an input stream is used for reading from a file.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    does ioException.getMessage() print the stacktrace?

    or try ioException.printStackTrace()

    Show you exactly the problem line.


Advertisement