Class ArmyFileHandler
java.lang.Object
edu.ntnu.idatt2001.carljgu.filehandling.ArmyFileHandler
Handles writing, reading and importing
armies to and from a file.
- Version:
- 1.4 1.05.2022
- Author:
- Carl Gützkow
-
Field Summary
-
Constructor Summary
ConstructorDescriptionConstructor which initiates the readLinesSkipped arraylist Sets the delimiter to a comma for comma seperated value. -
Method Summary
Modifier and TypeMethodDescriptiongetFilePath
(String fileChooserType) Uses the system default file explorer to get a file path of the user's choice.Gets the amount of units that were skipped when reading from file.readArmyFromFile
(String filePath) Finds a specified csv file and converts it to an army if it can.void
writeToFile
(Army army, String filePath) Creates a file to write on or writes to an existing file.
-
Field Details
-
readLinesSkipped
-
DELIMITER
-
-
Constructor Details
-
ArmyFileHandler
public ArmyFileHandler()Constructor which initiates the readLinesSkipped arraylist Sets the delimiter to a comma for comma seperated value.
-
-
Method Details
-
getReadLinesSkipped
Gets the amount of units that were skipped when reading from file.- Returns:
- readLinesSkipped - amount of lines skipped when reading army from file because of invalid unit.
-
writeToFile
public void writeToFile(Army army, String filePath) throws NullPointerException, FileExtensionException, IOException Creates a file to write on or writes to an existing file. The client should handle any exception that is thrown with an error message the client creates.- Parameters:
army
- Army - A collection of units that is written to a filefilePath
- String - name of the file and its full path- Throws:
NullPointerException
- thrown if the army is a null objectFileExtensionException
- thrown if the file extension is not csv because of the file nameIOException
- thrown if an I/O error occurs
-
readArmyFromFile
public Army readArmyFromFile(String filePath) throws IOException, FileNotFoundException, IllegalArgumentException Finds a specified csv file and converts it to an army if it can. If there is a line which has an unreadable unit it will continue on the next line The client should handle any exception that is thrown with an error message the client creates.- Parameters:
filePath
- - String - name of the file path to read from- Returns:
- army - Army - an army read from a csv file
- Throws:
IOException
- thrown if an I/O error occursFileNotFoundException
- thrown if the filepath was not foundIllegalArgumentException
- thrown if file extension is not csv or if army could not be created.
-
getFilePath
Uses the system default file explorer to get a file path of the user's choice. If a user closes the file chooser, null is returned.- Parameters:
fileChooserType
- String - OPEN or SAVE depending on whether the user wants to open a file or write to a file- Returns:
- filePath - String - the file path of a csv file for an army
-