Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Nicholas Pipitone
- * OOP Block 1
- * 12-14-2015
- */
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Date;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Main {
- private Scanner scan = new Scanner(System.in);
- public void run() {
- try {
- String dir = "C:\\Users\\25411307\\Documents\\Number 31.txt";
- File f = new File(dir);
- if (f.exists()) {
- System.out.println("Last Edited: " + (new Date(f.lastModified())) + "\nData: \n");
- BufferedReader br = new BufferedReader(new FileReader(dir));
- String line = null;
- while ((line = br.readLine()) != null) {
- System.out.println(line);
- }
- System.out.println("\nDo you want to append? (y/n)");
- } else {
- f.getParentFile().mkdirs();
- f.createNewFile();
- System.out.println("File did not exist and has been created.\n"
- +"Do you want to write data? (y/n)");
- }
- String s = scan.nextLine();
- while (s.length() == 0) {
- s = scan.nextLine();
- }
- if (s.charAt(0) == 'y') {
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(dir, true)));
- out.println(scan.nextLine());
- out.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("<End Program>");
- }
- }
- /* OUTPUT:
- File did not exist and has been created.
- Do you want to write data? (y/n)
- y
- Hello!
- <End Program>
- Last Edited: Mon Dec 14 21:31:13 EST 2015
- Data:
- Hello!
- Do you want to append? (y/n)
- y
- Bye!
- <End Program>
- Last Edited: Mon Dec 14 21:31:45 EST 2015
- Data:
- Hello!
- Bye!
- Do you want to append? (y/n)
- n
- <End Program>
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement