Advertisement
earlution

ReadWriteAppendToTxtFile

Oct 4th, 2021 (edited)
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Demonstration of how to create, read from, write and append to text files, using a menu-based approach.
  5.  *
  6.  * @author (Ruari Mears)
  7.  * @version (04.01.2021)
  8.  */
  9. public class ReadWriteAppendToTxtFile
  10. {
  11.     private Scanner scanner = new Scanner(System.in);
  12.     private FileHandler fileHandler = new FileHandler();
  13.    
  14.     public static void main(String[] args){
  15.         ReadWriteAppendToTxtFile topic06 = new ReadWriteAppendToTxtFile();
  16.         topic06.menu();
  17.     }
  18.  
  19.     private void menu(){
  20.         System.out.println("1\t Create a text file");
  21.         System.out.println("2\t Read from text file");
  22.         System.out.println("3\t Write to file");
  23.         System.out.println("4\t Append to file");
  24.         System.out.println();
  25.         System.out.println("Please enter your choice:");
  26.  
  27.         int choice = scanner.nextInt();
  28.  
  29.         switch (choice) {
  30.             case 1:
  31.                 System.out.println("Creating text file...");
  32.                 fileHandler.createFile();
  33.                 break;
  34.             case 2:
  35.                 System.out.println("Reading from text file...");
  36.                 fileHandler.readFromFile();
  37.                 break;
  38.             case 3:
  39.                 System.out.println("Writing to text file...");
  40.                 fileHandler.writeToFile();
  41.                 break;
  42.             case 4:
  43.                 System.out.println("Appending to text file...");
  44.                 fileHandler.appendToFile();
  45.                 break;
  46.             default: System.out.println("Invalid choice");
  47.         }
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement