Package app.service
Class FileOperations
java.lang.Object
app.service.FileOperations
public class FileOperations
extends java.lang.Object
A class containing operations and logic for choosing, reading and writing to files.
-
Method Summary
Modifier and Type Method Description static java.io.File
openDirectoryWithDialog(javafx.stage.Stage stage)
A function to get directory through a dialogstatic java.io.File
openFileWithDialog(javafx.stage.Stage stage)
A function to get a file through a dialogstatic java.lang.String
readFile(java.nio.file.Path filePath)
Tries to read the contents of a file in the specified filepath.static boolean
saveFile(java.nio.file.Path filepath, java.lang.String content)
Saves a file a the specified filepath with the specified content.static boolean
saveFileWithDialog(javafx.stage.Stage stage, java.lang.String content)
Lets the user choose a file to save the specified content intoMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Method Details
-
openFileWithDialog
public static java.io.File openFileWithDialog(javafx.stage.Stage stage) throws java.io.FileNotFoundExceptionA function to get a file through a dialog- Parameters:
stage
- A JavaFX stage is required to show the dialog- Returns:
- The chosen file
- Throws:
java.io.FileNotFoundException
- if the dialog was canceled
-
openDirectoryWithDialog
public static java.io.File openDirectoryWithDialog(javafx.stage.Stage stage) throws java.io.FileNotFoundExceptionA function to get directory through a dialog- Parameters:
stage
- A JavaFX stage is required to show the dialog- Returns:
- The chosen directory
- Throws:
java.io.FileNotFoundException
- if the dialog was canceled
-
saveFile
public static boolean saveFile(java.nio.file.Path filepath, java.lang.String content)Saves a file a the specified filepath with the specified content. Shows an error message to the user if the file was not found at the specified location.- Parameters:
filepath
- The path of the file to save the content intocontent
- The text content to be saved- Returns:
- Whether the file was sucessfully saved
-
saveFileWithDialog
public static boolean saveFileWithDialog(javafx.stage.Stage stage, java.lang.String content)Lets the user choose a file to save the specified content into- Parameters:
stage
- A JavaFX stage is needed in order to show the file choosercontent
- The content to be saved- Returns:
- Whether the file was sucessfully saved
-
readFile
public static java.lang.String readFile(java.nio.file.Path filePath)Tries to read the contents of a file in the specified filepath. If it fails, it shows the user a corresponding error message.- Parameters:
filePath
- The path of the file to be read- Returns:
- The text content of the file
-