Advertisement
Nickpips

Untitled

Dec 14th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. /*
  2. * Nicholas Pipitone
  3. * OOP Block 1
  4. * 12-14-2015
  5. */
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.BufferedWriter;
  9. import java.io.File;
  10. import java.io.FileReader;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.io.PrintWriter;
  14. import java.util.Date;
  15. import java.util.InputMismatchException;
  16. import java.util.Scanner;
  17.  
  18. public class Main {
  19. private Scanner scan = new Scanner(System.in);
  20.  
  21. public void run() {
  22. try {
  23. String dir = "C:\\Users\\25411307\\Documents\\Number 31.txt";
  24.  
  25. File f = new File(dir);
  26. if (f.exists()) {
  27. System.out.println("Last Edited: " + (new Date(f.lastModified())) + "\nData: \n");
  28. BufferedReader br = new BufferedReader(new FileReader(dir));
  29. String line = null;
  30. while ((line = br.readLine()) != null) {
  31. System.out.println(line);
  32. }
  33. System.out.println("\nDo you want to append? (y/n)");
  34. } else {
  35. f.getParentFile().mkdirs();
  36. f.createNewFile();
  37. System.out.println("File did not exist and has been created.\n"
  38. +"Do you want to write data? (y/n)");
  39. }
  40. String s = scan.nextLine();
  41. while (s.length() == 0) {
  42. s = scan.nextLine();
  43. }
  44. if (s.charAt(0) == 'y') {
  45. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(dir, true)));
  46. out.println(scan.nextLine());
  47. out.close();
  48. }
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. }
  52.  
  53. System.out.println("<End Program>");
  54. }
  55. }
  56.  
  57. /* OUTPUT:
  58. File did not exist and has been created.
  59. Do you want to write data? (y/n)
  60. y
  61. Hello!
  62. <End Program>
  63. Last Edited: Mon Dec 14 21:31:13 EST 2015
  64. Data:
  65.  
  66. Hello!
  67.  
  68. Do you want to append? (y/n)
  69. y
  70. Bye!
  71. <End Program>
  72. Last Edited: Mon Dec 14 21:31:45 EST 2015
  73. Data:
  74.  
  75. Hello!
  76. Bye!
  77.  
  78. Do you want to append? (y/n)
  79. n
  80. <End Program>
  81. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement