By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Having trouble writing to a file

P: 11
Hey all

I have been working on a project which allows me to write to a file and retrieve from that file, but i having trouble with it

These are my inputs:
private int flightno;
private int sourceairportcode;
private int destinationaiportcode;
private String takeofftime;
private String landingtime;

These are variables at the top declared
ObjectOutputStream output;
ObjectInputStream input;

i have this in my code

File file = new File(System.getProperty("user.dir") + "/data/flights.txt");
(not sure if this is the problem maybe its not writing to the correct place, i have created the data folder with an empty flights.txt file in it)


and here is what aint working

private void btnaddMousePressed(java.awt.event.MouseEvent evt) {
FlightRecord record;

try{

record = new FlightRecord(

Integer.parseInt(txtflightno.getText()),

Integer.parseInt(txtsource.getText()),

Integer.parseInt(txtdestination.getText()),

txttakeoff.getText(),

txtlanding.getText());

output.writeObject(record);

}

catch (Exception e){
JOptionPane.showMessageDialog(this, "Something wrong!", "Error",JOptionPane.ERROR_MESSAGE );
}


Any help would be great.
Aug 27 '07 #1
Share this Question
Share on Google+
12 Replies


Nepomuk
Expert 2.5K+
P: 3,112
Are you using Windows? If so, one mistake (maybe not the only, but certainly one, which would cause problems) is here:
File file = new File(System.getProperty("user.dir") + "/data/flights.txt");
The File seperator under Windows is \ (System.getProperty("file.separator") or File.seperator). You should use those two anyway, to provide independency from the OS.
If you don't want to use either of those, use:
Expand|Select|Wrap|Line Numbers
  1. File file = new File(System.getProperty("user.dir") + "\\data\\flights.txt");
Aug 27 '07 #2

10K+
P: 13,264
1.) Please use code tags when posting code
2.) I'm not sure I get the exact problem you're getting but this might help.
Aug 27 '07 #3

P: 11
Hmm messed about with the file seperator stuff didnt seem to change anything, got to be a problem somewhere else.
Aug 27 '07 #4

10K+
P: 13,264
Hmm messed about with the file seperator stuff didnt seem to change anything, got to be a problem somewhere else.
Could you post the full stacktrace or error message that you're getting?
Aug 27 '07 #5

P: 11
Im using netbeans to do it, and the try and catch blocks just say somethings wrong when i try and add the info to the file,. not sure where i can find the full error message report
Aug 27 '07 #6

10K+
P: 13,264
Im using netbeans to do it, and the try and catch blocks just say somethings wrong when i try and add the info to the file,. not sure where i can find the full error message report
In the catch block just put
Expand|Select|Wrap|Line Numbers
  1. e.printStackTrace();
replace e with your exception variable name and then watch the console window when running it to see the fireworks.
Aug 27 '07 #7

P: 11
think its something to do with the directopry of the file its being saved to

File file = new File(System.getProperty("user.dir") + "\\data\\flights.txt");

my flights file is premade and is in

C:\Documents and Settings\Fozzi\assignment7\data\flights.txt

is that the correct path? maybe i need to add something extra
Aug 27 '07 #8

10K+
P: 13,264
think its something to do with the directopry of the file its being saved to

File file = new File(System.getProperty("user.dir") + "\\data\\flights.txt");

my flights file is premade and is in

C:\Documents and Settings\Fozzi\assignment7\data\flights.txt

is that the correct path? maybe i need to add something extra
What's your current working directory?
You can also print the value of
System.getProperty("user.dir") + "\\data\\flights.txt" to the console to see if it's path you intended.
Aug 27 '07 #9

P: 11
My current working directory is

C:\Documents and Settings\Fozzi\assignment7

assignment 7 being the main project folder that i am working from there are several folders within and i created the data folder with the txt file in it, in assignment 7
Aug 27 '07 #10

10K+
P: 13,264
My current working directory is

C:\Documents and Settings\Fozzi\assignment7

assignment 7 being the main project folder that i am working from there are several folders within and i created the data folder with the txt file in it, in assignment 7
And what was printed out when you tried what I suggested above.
Another thing is that you haven't told us the exact exception/ error message that you're getting.
Aug 27 '07 #11

Expert 10K+
P: 11,448
I'm still interested in what that Exception e has to say.

kind regards,

Jos
Aug 27 '07 #12

Nepomuk
Expert 2.5K+
P: 3,112
You can check, if the File you gave exists by using:
Expand|Select|Wrap|Line Numbers
  1. File file = new File(System.getProperty("user.dir") + "\\data\\flights.txt");
  2. if(file.exists()) System.out.println("It's there!");
  3. else System.out.println("It's not there!");
Nevertheless, if this doesn't help, please do post the Stack Trace! ^^
Aug 28 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.