earlution

FileHandler

Oct 4th, 2021 (edited)
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.59 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.IOException;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.FileNotFoundException;
  6. import java.io.BufferedWriter;
  7.  
  8. /**
  9.  * Contains methods for creating, reading from, writing and appending to text files.
  10.  *
  11.  * @author (Ruari Mears)
  12.  * @version (04.10.2021)
  13.  */
  14. public class FileHandler
  15. {
  16.     private Scanner scanner;
  17.  
  18.     /**
  19.      * Constructor for objects of class FileHandler
  20.      */
  21.     public FileHandler()
  22.     {
  23.         scanner = new Scanner(System.in);
  24.     }
  25.  
  26.     private String getFilename() {
  27.         Scanner scanner = new Scanner(System.in);
  28.         System.out.println();
  29.         System.out.println("Please specify the filename:");
  30.         String filename = scanner.next();
  31.  
  32.         return filename;
  33.     }
  34.  
  35.     /**
  36.      * Source: https://www.w3schools.com/java/java_files_create.asp
  37.      *
  38.      * To create a file in Java, you can use the createNewFile() method.
  39.      * This method returns a boolean value: true if the file was successfully created, and false if the file
  40.      * already exists. Note that the method is enclosed in a try...catch block. This is necessary because it
  41.      * throws an IOException if an error occurs (if the file cannot be created for some reason)
  42.      */
  43.     public void createFile() {
  44.         String filename = getFilename();
  45.         //String path = "..\\FileHandling\\";
  46.         try {
  47.             //File file = new File(path + filename);
  48.             File fileObject = new File(filename);
  49.             if (fileObject.createNewFile()) {
  50.                 //System.out.println("File created: " + file.getName());
  51.                 System.out.println(fileObject.getName());
  52.             } else {
  53.                 System.out.println("File already exists.");
  54.             }
  55.         } catch (IOException e) {
  56.             System.out.println("An error occurred.");
  57.             e.printStackTrace();
  58.         }
  59.     }
  60.  
  61.     public void readFromFile() {
  62.         try {
  63.             String filename = getFilename();
  64.             File fileObject = new File(filename);
  65.             Scanner myReader = new Scanner(fileObject);
  66.             while (myReader.hasNextLine()) {
  67.                 String data = myReader.nextLine();
  68.                 System.out.println(data);
  69.             }
  70.             myReader.close();
  71.         } catch (FileNotFoundException e) {
  72.             System.out.println("An error occurred.");
  73.             e.printStackTrace();
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * Source: https://www.w3schools.com/java/java_files_create.asp
  79.      *
  80.      * In the following example, we use the FileWriter class together with its write() method to write some
  81.      * text to the file we created in the example above.
  82.      * Note that when you are done writing to the file, you should close it with the close() method.
  83.      */
  84.     public void writeToFile() {
  85.         try {
  86.             String filename = getFilename();
  87.             FileWriter fileWriter = new FileWriter(filename);
  88.             fileWriter.write("Writing to files in Java might seem tricky, but it is easy enough!");
  89.             fileWriter.close();
  90.             System.out.println("Successfully wrote to the file.");
  91.         } catch (IOException e) {
  92.             System.out.println("An error occurred.");
  93.             e.printStackTrace();
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * Source: https://www.tutorialspoint.com/Java-Program-to-Append-Text-to-an-Existing-File
  99.      *
  100.      * Uses the Java.io.BufferedWriter class writes text to a character-output stream, buffering characters so
  101.      *  as to provide for the efficient writing of single characters, arrays, and strings.
  102.      *  
  103.      * To add contents to a file:
  104.      *
  105.      * - Instantiate the BufferedWriter class.
  106.      * - By passing the FileWriter object as an argument to its constructor.
  107.      * - Write data to the file using the write() method.
  108.      */
  109.     public void appendToFile() {
  110.         try {
  111.             String filename = getFilename();
  112.             String data = "\n" + "This is a line of new text added " + java.time.LocalDate.now() + " "
  113.                 + java.time.LocalTime.now();
  114.             File fileObject = new File(filename);
  115.             if(!fileObject.exists()) {
  116.                 fileObject.createNewFile();
  117.             }
  118.  
  119.             FileWriter fileWritter = new FileWriter(fileObject.getName(),true);
  120.             BufferedWriter bufferedWriter = new BufferedWriter(fileWritter);
  121.             bufferedWriter.write(data);
  122.             bufferedWriter.close();
  123.             System.out.println("Done");
  124.         } catch(IOException e){
  125.             e.printStackTrace();
  126.         }
  127.     }
  128. }
  129.  
Add Comment
Please, Sign In to add comment