Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Alvin Bello
- * OOP - Period 6
- * File IO Lab 2
- */
- package main;
- import java.util.*;
- import java.io.*;
- public class Main {
- static ArrayList<File> files = new ArrayList<File>();
- boolean cont;
- static Scanner scan = new Scanner(System.in);
- PrintWriter write;
- // thrown because I don't want to deal with such garbage.
- public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException {
- Main test = new Main();
- test.run();
- }
- // accepts integers between bounds a and b.
- public static int getInt(int a, int b) {
- int input = getInt();
- while (input < a || input > b) {
- System.out.println("You must pick a number in-between " + a + " and " + b + "!");
- input = getInt();
- }
- return input;
- }
- // try-catch entering an integer.
- public static int getInt() {
- int output = 0;
- while (true) {
- try {
- output = scan.nextInt();
- } catch (InputMismatchException e) {
- System.out.printf("Please input an integer.\n");
- scan.nextLine();
- continue;
- } catch (Exception e) {
- System.out.println("Exception: " + e);
- return -1;
- }
- break;
- }
- scan.nextLine();
- return output;
- }
- // runs the menu and operations. Threw these here also because safety first.
- public void run() throws FileNotFoundException, UnsupportedEncodingException, IOException {
- cont = false;
- int input = 0;
- for( File f : (new File("./files/")).listFiles() )
- files.add(f);
- do {
- System.out.println(
- "If you would like to exit, enter 0.\n"
- + "If you would like to create a file, enter 1.\n"
- + "If you would like to write to a file, enter 2.\n"
- + "If you would like to read a file, enter 3.\n");
- switch (getInt(0, 3)) {
- case 0:
- cont = false;
- break;
- case 1:
- System.out.println("Enter the file name.");
- File file = new File("./files/" + scan.nextLine() + ".txt");
- if (!file.exists()) {
- file.createNewFile();
- files.add(file);
- System.out.println("The file has been created.\n");
- } else {
- System.out.println("A file already exists with that name.");
- }
- cont = true;
- break;
- case 2:
- if (!(files.isEmpty())) {
- System.out.println("What file would you like to write to? Enter the integer next to it.");
- for (int i = 0; i < files.size(); i++) {
- System.out.println("[" + i + "] " + files.get(i).toString().substring(8));
- }
- input = getInt(0, files.size() - 1);
- write = new PrintWriter(files.get(input), "UTF-8");
- System.out.println("You may now write to the file.\n");
- write.println(scan.nextLine());
- write.close();
- System.out.println("\nYou have written to the file.\n");
- } else {
- System.out.println("There are no files to write to. Create one before writing to it.\n");
- }
- cont = true;
- break;
- case 3:
- if (!(files.isEmpty())) {
- System.out.println("What file would you like to read from? Enter the integer next to it.");
- for (int i = 0; i < files.size(); i++) {
- System.out.println("[" + i + "] " + files.get(i).toString().substring(8));
- }
- input = getInt(0, files.size() - 1);
- Scanner read = new Scanner(files.get(input));
- while (read.hasNext()) {
- System.out.println(read.nextLine());
- }
- read.close();
- System.out.println("\nYou have read from the file.");
- } else {
- System.out.println("There are no files to read from. Create one before reading from it.");
- }
- cont = true;
- break;
- default:
- System.out.println("Invalid operation, try again.");
- cont = true;
- }
- } while (cont);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement