Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.IOException;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.FileNotFoundException;
- import java.io.BufferedWriter;
- /**
- * Contains methods for creating, reading from, writing and appending to text files.
- *
- * @author (Ruari Mears)
- * @version (04.10.2021)
- */
- public class FileHandler
- {
- private Scanner scanner;
- /**
- * Constructor for objects of class FileHandler
- */
- public FileHandler()
- {
- scanner = new Scanner(System.in);
- }
- private String getFilename() {
- Scanner scanner = new Scanner(System.in);
- System.out.println();
- System.out.println("Please specify the filename:");
- String filename = scanner.next();
- return filename;
- }
- /**
- * Source: https://www.w3schools.com/java/java_files_create.asp
- *
- * To create a file in Java, you can use the createNewFile() method.
- * This method returns a boolean value: true if the file was successfully created, and false if the file
- * already exists. Note that the method is enclosed in a try...catch block. This is necessary because it
- * throws an IOException if an error occurs (if the file cannot be created for some reason)
- */
- public void createFile() {
- String filename = getFilename();
- //String path = "..\\FileHandling\\";
- try {
- //File file = new File(path + filename);
- File fileObject = new File(filename);
- if (fileObject.createNewFile()) {
- //System.out.println("File created: " + file.getName());
- System.out.println(fileObject.getName());
- } else {
- System.out.println("File already exists.");
- }
- } catch (IOException e) {
- System.out.println("An error occurred.");
- e.printStackTrace();
- }
- }
- public void readFromFile() {
- try {
- String filename = getFilename();
- File fileObject = new File(filename);
- Scanner myReader = new Scanner(fileObject);
- while (myReader.hasNextLine()) {
- String data = myReader.nextLine();
- System.out.println(data);
- }
- myReader.close();
- } catch (FileNotFoundException e) {
- System.out.println("An error occurred.");
- e.printStackTrace();
- }
- }
- /**
- * Source: https://www.w3schools.com/java/java_files_create.asp
- *
- * In the following example, we use the FileWriter class together with its write() method to write some
- * text to the file we created in the example above.
- * Note that when you are done writing to the file, you should close it with the close() method.
- */
- public void writeToFile() {
- try {
- String filename = getFilename();
- FileWriter fileWriter = new FileWriter(filename);
- fileWriter.write("Writing to files in Java might seem tricky, but it is easy enough!");
- fileWriter.close();
- System.out.println("Successfully wrote to the file.");
- } catch (IOException e) {
- System.out.println("An error occurred.");
- e.printStackTrace();
- }
- }
- /**
- * Source: https://www.tutorialspoint.com/Java-Program-to-Append-Text-to-an-Existing-File
- *
- * Uses the Java.io.BufferedWriter class writes text to a character-output stream, buffering characters so
- * as to provide for the efficient writing of single characters, arrays, and strings.
- *
- * To add contents to a file:
- *
- * - Instantiate the BufferedWriter class.
- * - By passing the FileWriter object as an argument to its constructor.
- * - Write data to the file using the write() method.
- */
- public void appendToFile() {
- try {
- String filename = getFilename();
- String data = "\n" + "This is a line of new text added " + java.time.LocalDate.now() + " "
- + java.time.LocalTime.now();
- File fileObject = new File(filename);
- if(!fileObject.exists()) {
- fileObject.createNewFile();
- }
- FileWriter fileWritter = new FileWriter(fileObject.getName(),true);
- BufferedWriter bufferedWriter = new BufferedWriter(fileWritter);
- bufferedWriter.write(data);
- bufferedWriter.close();
- System.out.println("Done");
- } catch(IOException e){
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment