Java Saving Flight Data to File: A Method for Storing Flight Information in Java

  • Thread starter Thread starter JaysFan31
  • Start date Start date
  • Tags Tags
    File
AI Thread Summary
The discussion focuses on creating a method named "save" that takes a filename as a parameter and returns a boolean indicating success or failure in saving a Flight object to a file. The method is designed to write the flight's details, including the flight number, origin and destination airports, and a list of passengers, to the specified file. The provided code compiles and returns true, but the user is uncertain if the file is actually created. A suggestion is made to check the filesystem to confirm file creation. Additionally, an alternative approach using Java's serialization mechanism is recommended, as it simplifies the process of saving and restoring objects without manual coding.
JaysFan31
I need to write a method called save that takes a String as a parameter, and returns a boolean value. The parameter represents a filename, and the method should save the contents of one of my classes Flight.java to a file with that filename. I need to include all of the contents created in Flight.java, including the origin and destination airports, as well as an entire list of passengers. I need to able to restore the Flight object and all of its data from the file. Thus, I need to ensure that all of the data gets saved to the file. The method should return true if it is able to save the flight, or false otherwise.

Here's my code:
Code:
public boolean save(String fileName) throws IOException
  {    
    FileWriter fw = new FileWriter (fileName,true);
    BufferedWriter bw = new BufferedWriter (fw);
    PrintWriter outFile = new PrintWriter (bw);    
    outFile.println(FlightNumber);
    outFile.println(OriginalAirport);
    outFile.println(DestinationAirport);
    for (int i = 0; i < numberOfPassengers; i++)
      outFile.println(passengers[i].toString() + "\n");
    outFile.close();    
    return true;    
  }

It compiles and obviously in the main method, it prints true, but I don't know if it actually creates a file.
I'm new to this file material and I'm just not sure if this works or not or even if I'm on the right track.

Thanks.
Michael
 
Technology news on Phys.org
Well, you should be able to look at your filesystem and see if it created the file! It looks like you're on the right track.

However, I should mention that Java includes a "serialization" mechanism that can automatically save and restore objects to files without any effort from you. You should consider using serialization, unless you have some specific reason it cannot be used.

- Warren
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
10
Views
1K
Replies
1
Views
1K
Replies
3
Views
3K
Replies
3
Views
3K
Replies
3
Views
4K
Replies
2
Views
4K
Replies
4
Views
2K
Replies
6
Views
3K
Back
Top