Advertisement
Guest User

Text Editor

a guest
Feb 9th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. /**
  2.  * Created by Robby on 2/8/2016.
  3.  */
  4.  
  5. import java.util.Scanner;
  6. import java.io.*;
  7.  
  8. public class textEditor
  9. {
  10.     public static void main(String[] args)throws IOException
  11.     {
  12.         Scanner scan = new Scanner( System.in );
  13.         Scanner fileIn;
  14.         File fileOut;
  15.         PrintStream print;
  16.  
  17.         boolean fileNameCheck;
  18.  
  19.         String input = "";
  20.         String fileName = "";
  21.         String baseName = "";
  22.  
  23.         int fileNumber;
  24.  
  25.  
  26.         do {
  27.             fileNameCheck = false;
  28.  
  29.             do {
  30.                 System.out.print("What would you like to name the document: ");
  31.                 fileName = scan.nextLine();
  32.             }while( fileName.isEmpty() );
  33.  
  34.             try {
  35.                 fileIn = new Scanner( new File( "Documents/" + fileName + ".txt" ) );
  36.             }catch(FileNotFoundException e){
  37.                 fileOut = new File( "Documents/" + fileName + ".txt" );
  38.                 print = new PrintStream( fileOut );
  39.                 fileIn = new Scanner( fileOut );
  40.             }
  41.  
  42.             if( fileIn.hasNext() )
  43.             {
  44.                 do {
  45.                     System.out.print("The file " + fileName + ".txt already exists and is not empty. Would you like to overwrite it (Y/n): ");
  46.                     input = scan.nextLine();
  47.                 }while( (!input.startsWith("y") && !input.startsWith("Y") && !input.startsWith("n") && !input.startsWith("N")) || input.isEmpty() );
  48.  
  49.                 if( input.startsWith("y") || input.startsWith("Y") )
  50.                     break;
  51.                 else {
  52.                     fileNumber = 0;
  53.                     baseName = fileName;
  54.                     do {
  55.                         fileNumber++;
  56.                         fileName = baseName + " (" + fileNumber + ")";
  57.  
  58.                         try {
  59.                             fileIn = new Scanner( new File( "Documents/" + fileName + ".txt" ) );
  60.                         }catch (FileNotFoundException e) {
  61.                             System.out.print("Is the name " + fileName + ".txt a good name (Y/n): ");
  62.                             input = scan.nextLine();
  63.  
  64.                             if ( input.startsWith("y") || input.startsWith("Y") )
  65.                                 break;
  66.                             else
  67.                                 fileNameCheck = true;
  68.                         }
  69.                     }while ( fileIn.hasNext() );
  70.                 }
  71.             }
  72.  
  73.         }while(fileNameCheck);
  74.  
  75.         fileOut = new File( "Documents/" + fileName + ".txt" );
  76.         print = new PrintStream( fileOut );
  77.  
  78.         System.out.println("You are now editing " + fileName + ".txt. Remember to use //done when you are finished.");
  79.  
  80.         input = "";
  81.         final String SENTINEL = "//done";
  82.  
  83.         while( !input.equals(SENTINEL) )
  84.         {
  85.             System.out.print("> ");
  86.             input = scan.nextLine();
  87.  
  88.             if(!input.equalsIgnoreCase(SENTINEL))
  89.                 print.println(input);
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement