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

  • Context: Java 
  • Thread starter Thread starter JaysFan31
  • Start date Start date
  • Tags Tags
    File
Click For Summary
SUMMARY

The discussion focuses on implementing a method in Java to save flight data to a file. The proposed method, named save, takes a filename as a parameter and writes the flight details, including flight number, origin, destination, and passenger list, to the specified file. While the initial implementation compiles successfully, the user is uncertain about the file creation process. Warren suggests considering Java's built-in serialization mechanism for automatic object saving and restoration, which may simplify the task.

PREREQUISITES
  • Java programming language knowledge
  • Understanding of file I/O operations in Java
  • Familiarity with Java's serialization mechanism
  • Basic concepts of object-oriented programming
NEXT STEPS
  • Explore Java FileWriter and BufferedWriter classes for file operations
  • Learn about Java Object Serialization and how to implement it
  • Research best practices for error handling in file I/O
  • Investigate how to read data back from files in Java
USEFUL FOR

Java developers, software engineers working with flight data management, and anyone interested in file handling and object serialization in Java.

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
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
52
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K