473,395 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Having trouble writing to a file

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
12 1903
Nepomuk
3,112 Expert 2GB
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
r035198x
13,262 8TB
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
Fozzi
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
r035198x
13,262 8TB
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
Fozzi
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
r035198x
13,262 8TB
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
Fozzi
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
r035198x
13,262 8TB
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
Fozzi
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
r035198x
13,262 8TB
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
JosAH
11,448 Expert 8TB
I'm still interested in what that Exception e has to say.

kind regards,

Jos
Aug 27 '07 #12
Nepomuk
3,112 Expert 2GB
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

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
12
by: Winbatch | last post by:
Hi, I'm trying to learn multithreading and it doesn't seem to be working for me. I have a feeling it has to do with the fact that I'm writing to files rather than to printf, but maybe not. ...
3
by: rdh | last post by:
I'm using List<> to store a class of positions. Position are basically latitude and longitudes (doubles). the line is something like List<position> P = new List<position>(); Then I start...
9
suzee_q00
by: suzee_q00 | last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it....
2
by: spidey12345 | last post by:
what i need this program to do is to read paragraphs like "st blah blh test ere se sit blha eere w" and then it will reformat to "st blah...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
0
grassh0pp3r
by: grassh0pp3r | last post by:
Hello, I'm trying to make a very simple comments page on my site using PHP and am having problems somewhere. I am very new to PHP. I was able to create one that works with comments appended, but...
0
by: UncleRic | last post by:
Environment: Mac OS X (10.4.10) on MacBook Pro I'm a Perl Neophyte. I've downloaded the XML::Parser module and am attempting to install it in my working directory (referenced via PERL5LIB env): ...
1
by: runningsnake24 | last post by:
We are writing a program to check that a filled in Sudoku puzzle is solved correctly. We are required to use the Iterator and Iterable interfaces. The program uses a Cell class to represent each...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.