Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Robby on 2/8/2016.
- */
- import java.util.Scanner;
- import java.io.*;
- public class textEditor
- {
- public static void main(String[] args)throws IOException
- {
- Scanner scan = new Scanner( System.in );
- Scanner fileIn;
- File fileOut;
- PrintStream print;
- boolean fileNameCheck;
- String input = "";
- String fileName = "";
- String baseName = "";
- int fileNumber;
- do {
- fileNameCheck = false;
- do {
- System.out.print("What would you like to name the document: ");
- fileName = scan.nextLine();
- }while( fileName.isEmpty() );
- try {
- fileIn = new Scanner( new File( "Documents/" + fileName + ".txt" ) );
- }catch(FileNotFoundException e){
- fileOut = new File( "Documents/" + fileName + ".txt" );
- print = new PrintStream( fileOut );
- fileIn = new Scanner( fileOut );
- }
- if( fileIn.hasNext() )
- {
- do {
- System.out.print("The file " + fileName + ".txt already exists and is not empty. Would you like to overwrite it (Y/n): ");
- input = scan.nextLine();
- }while( (!input.startsWith("y") && !input.startsWith("Y") && !input.startsWith("n") && !input.startsWith("N")) || input.isEmpty() );
- if( input.startsWith("y") || input.startsWith("Y") )
- break;
- else {
- fileNumber = 0;
- baseName = fileName;
- do {
- fileNumber++;
- fileName = baseName + " (" + fileNumber + ")";
- try {
- fileIn = new Scanner( new File( "Documents/" + fileName + ".txt" ) );
- }catch (FileNotFoundException e) {
- System.out.print("Is the name " + fileName + ".txt a good name (Y/n): ");
- input = scan.nextLine();
- if ( input.startsWith("y") || input.startsWith("Y") )
- break;
- else
- fileNameCheck = true;
- }
- }while ( fileIn.hasNext() );
- }
- }
- }while(fileNameCheck);
- fileOut = new File( "Documents/" + fileName + ".txt" );
- print = new PrintStream( fileOut );
- System.out.println("You are now editing " + fileName + ".txt. Remember to use //done when you are finished.");
- input = "";
- final String SENTINEL = "//done";
- while( !input.equals(SENTINEL) )
- {
- System.out.print("> ");
- input = scan.nextLine();
- if(!input.equalsIgnoreCase(SENTINEL))
- print.println(input);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement