Advertisement
Nickpips

Untitled

Nov 28th, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /* Alvin Bello
  2. * OOP - Period 6
  3. * File IO Lab 2
  4. */
  5. package main;
  6.  
  7. import java.util.*;
  8. import java.io.*;
  9.  
  10. public class Main {
  11.  
  12. static ArrayList<File> files = new ArrayList<File>();
  13. boolean cont;
  14. static Scanner scan = new Scanner(System.in);
  15. PrintWriter write;
  16.  
  17. // thrown because I don't want to deal with such garbage.
  18. public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException {
  19. Main test = new Main();
  20. test.run();
  21. }
  22.  
  23. // accepts integers between bounds a and b.
  24. public static int getInt(int a, int b) {
  25. int input = getInt();
  26. while (input < a || input > b) {
  27. System.out.println("You must pick a number in-between " + a + " and " + b + "!");
  28. input = getInt();
  29. }
  30. return input;
  31. }
  32.  
  33. // try-catch entering an integer.
  34. public static int getInt() {
  35. int output = 0;
  36. while (true) {
  37. try {
  38. output = scan.nextInt();
  39. } catch (InputMismatchException e) {
  40. System.out.printf("Please input an integer.\n");
  41. scan.nextLine();
  42. continue;
  43. } catch (Exception e) {
  44. System.out.println("Exception: " + e);
  45. return -1;
  46. }
  47. break;
  48. }
  49. scan.nextLine();
  50. return output;
  51. }
  52.  
  53. // runs the menu and operations. Threw these here also because safety first.
  54. public void run() throws FileNotFoundException, UnsupportedEncodingException, IOException {
  55. cont = false;
  56. int input = 0;
  57. for( File f : (new File("./files/")).listFiles() )
  58. files.add(f);
  59.  
  60. do {
  61. System.out.println(
  62. "If you would like to exit, enter 0.\n"
  63. + "If you would like to create a file, enter 1.\n"
  64. + "If you would like to write to a file, enter 2.\n"
  65. + "If you would like to read a file, enter 3.\n");
  66. switch (getInt(0, 3)) {
  67. case 0:
  68. cont = false;
  69. break;
  70. case 1:
  71. System.out.println("Enter the file name.");
  72. File file = new File("./files/" + scan.nextLine() + ".txt");
  73. if (!file.exists()) {
  74. file.createNewFile();
  75. files.add(file);
  76. System.out.println("The file has been created.\n");
  77. } else {
  78. System.out.println("A file already exists with that name.");
  79. }
  80. cont = true;
  81. break;
  82. case 2:
  83. if (!(files.isEmpty())) {
  84. System.out.println("What file would you like to write to? Enter the integer next to it.");
  85. for (int i = 0; i < files.size(); i++) {
  86. System.out.println("[" + i + "] " + files.get(i).toString().substring(8));
  87. }
  88. input = getInt(0, files.size() - 1);
  89. write = new PrintWriter(files.get(input), "UTF-8");
  90. System.out.println("You may now write to the file.\n");
  91. write.println(scan.nextLine());
  92. write.close();
  93. System.out.println("\nYou have written to the file.\n");
  94. } else {
  95. System.out.println("There are no files to write to. Create one before writing to it.\n");
  96. }
  97. cont = true;
  98. break;
  99. case 3:
  100. if (!(files.isEmpty())) {
  101. System.out.println("What file would you like to read from? Enter the integer next to it.");
  102. for (int i = 0; i < files.size(); i++) {
  103. System.out.println("[" + i + "] " + files.get(i).toString().substring(8));
  104. }
  105. input = getInt(0, files.size() - 1);
  106. Scanner read = new Scanner(files.get(input));
  107. while (read.hasNext()) {
  108. System.out.println(read.nextLine());
  109. }
  110. read.close();
  111. System.out.println("\nYou have read from the file.");
  112. } else {
  113. System.out.println("There are no files to read from. Create one before reading from it.");
  114. }
  115. cont = true;
  116. break;
  117. default:
  118. System.out.println("Invalid operation, try again.");
  119. cont = true;
  120. }
  121. } while (cont);
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement