Can someone put this code in a zip file I tried running it l

Can someone put this code in a zip file. I tried running it last time it was answered but it did not work.. Please and thanks in advance..

In my template for how to solve JH2, I assume you have seen the StringTokenizer which should be covered in cps161. You can find it in the first 9 chapters of \"Absolute Java\" by Savitch. The StringTokenizer is a simple class for parsing a String that has multiple words on it. Run the following simple example to see what it does:

class participation: 5 points

file_operations(45 points)

Create a package named file_operations and a class named FileOperations which will receive one or more command line pathnames to command files. Each command file will be opened and processed line by line. Each line can contain one of the following commands:

? - This command will print out the legal commands available

createFile - The first string following \"createFile\" will be treated as a filename, and the remaining strings will be written to the file separated by new lines.

printFile - The first string following \"printFile\" will be treated as a filename that will be opened up and printed out to the screen.

lastModified - The first string following \"lastModified\" will be treated as a filename for which we will print out the date when this file was last modified.

size - The first string following \"size\" will be treated as a filename for which we will print out the number of bytes it contains.

rename - The first string following \"rename\" will be treated as the current filename and the second string will be treated as the new filename we desire.

mkdir - The first string following \"mkdir\" will be treated as the name of a directory that should be created.

delete - The first string following \"delete\" will be treated as the name of a file that should be deleted.

list - The first string following \"list\" will be treated as the name of a directory for which we want a list of the files it contains.

quit - exit program

Anything else is a bad command

For a non-existent command file, you would get something like:

For a good command file like cmd.txt

cmd.txt

Your output would look something like:

Here is a template that you can use.

Once you have your program written, I want you to add the following files to the top level of your project and run the test that I request:

cmd1.txt

cmd2.txt

Run your program with the following command line:

cmd1.txt non-existent-file cmd2.txt

Insert the contents of your screen into the appropriate JH2 worksheet.

Please don\'t create a file with the name non-existent-file!!!

Solution

Hi,

PFB the class for the question. Please comment for any queries/feedbacks.

Thanks,

Anita

FileOperations.java

package file_operations;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.StringTokenizer;


// figure out your imports

public class FileOperations
{
   StringTokenizer parseCommand;
   private String currentLine;


   public void delete()
   {
       // code for handling the delete command
       // Make sure you check the return code from the
       // File delete method to print out success/failure status

       parseCommand = new StringTokenizer(currentLine);
       String command = getNextToken();
       String filename = getNextToken();
       File file = new File(filename);
       boolean isDeleted = file.delete();
       if(isDeleted){
           System.out.println(\"File is deleted.\");
       }
       else{
           System.out.println(\"Error in deleting the file\");
       }
   }
   public void rename()
   {
       // code for handling the rename command
       // Make sure you check the return code from the
       // File rename method to print out success/failure status
       parseCommand = new StringTokenizer(currentLine);
       String command = getNextToken();
       String filename = getNextToken();
       String newFileName = getNextToken();

       File oldName = new File(filename);
       File newName = new File(newFileName);

       if (newName.exists()) newName.delete();
       oldName.renameTo(newName);
       System.out.println(\"renamed\");

   }
   public void list()
   {
       // code for handling the list command
       parseCommand = new StringTokenizer(currentLine);
       String command = getNextToken();
       String directoryName = getNextToken();
       File folder = new File(directoryName);

       File[] listOfFiles = folder.listFiles();

       for (int i = 0; i < listOfFiles.length; i++) {
           if (listOfFiles[i].isFile()) {
               System.out.println(\"File \" + listOfFiles[i].getName());
           } else if (listOfFiles[i].isDirectory()) {
               System.out.println(\"Directory \" + listOfFiles[i].getName());
           }
       }
   }
   public void size()
   {
       // code for handling the size command
       parseCommand = new StringTokenizer(currentLine);
       String command = getNextToken();
       String filename = getNextToken();
       File file = new File(filename);

       System.out.println(\"Size in bytes of \"+filename+\" is \" + file.length());
   }
   public void lastModified()
   {
       // code for handling the lastModified command
       parseCommand = new StringTokenizer(currentLine);
       String command = getNextToken();
       String filename = getNextToken();
       File file = new File(filename);

       SimpleDateFormat sdf = new SimpleDateFormat(\"MM/dd/yyyy HH:mm:ss\");

       System.out.println(\"Last Modified of \"+filename+\" is \" + sdf.format(file.lastModified()));
   }
   public void mkdir()
   {
       // code for handling the mkdir command
       // Make sure you check the return code from the
       // File mkdir method to print out success/failure status
       parseCommand = new StringTokenizer(currentLine);
       String command = getNextToken();
       String directoryName = getNextToken();

       File file = new File(directoryName);
       if (!file.exists()) {
           if (file.mkdir()) {
               System.out.println(\"Directory is created!\");
           } else {
               System.out.println(\"Failed to create directory!\");
           }
       }
   }
   public void createFile()
   {
       // code for handling the createFile command
       parseCommand = new StringTokenizer(currentLine);
       String command = getNextToken();
       String filename = getNextToken();
       FileWriter fw;
       try {
           fw = new FileWriter(filename);
           String nextLine= getNextToken();
           while(null!= nextLine){
               //fw.append(\"\ \"); to print to a newline
               fw.write(nextLine);
               nextLine= getNextToken();
           }

           fw.close();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }


       System.out.println(filename+\" : File has been created. \");

   }

   public void printFile()
   {
       // code for handling the printFile command

       parseCommand = new StringTokenizer(currentLine);
       String command = getNextToken();
       String filename = getNextToken();
       System.out.println(\"Printing the contents of the file \"+filename+\" : \");
       try {


           FileReader fr = new FileReader(filename);
           BufferedReader br = new BufferedReader(fr);   
           // Read br and store a line in \'data\', print data
           String data;
           while((data = br.readLine()) != null)
           {
               //data = br.readLine( );   
               System.out.println(data);
           }
       } catch(IOException e) {
           e.printStackTrace();
       }
   }

   void printUsage()
   {
       // process the \"?\" command
   }


   // useful private routine for getting next string on the command line
   private String getNextToken()
   {
       if (parseCommand.hasMoreTokens())
           return parseCommand.nextToken();
       else
           return null;
   }

   // useful private routine for getting a File class from the next string on the command line
   private File getFile()
   {
       File f = null;
       String fileName = getNextToken();
       if (fileName == null)   
           System.out.println(\"Missing a File name\");
       else
           f = new File(fileName);

       return f;
   }

   public boolean processCommandLine(String line)
   {
       // if line is not null, then setup the parseCommand StringTokenizer. Pull off the first string
       // using getNextToken() and treat it as the \"command\" to be processed.

       // It would be good to print out the command you are processing to make your output more readable.

       // If you are using at least java 1.7, you can use a switch statement on command.
       // Otherwise, resort to if-then-else logic. Call the appropriate routine to process the requested command:
       // i.e. delete, rename, mkdir list, etc.
       // return false if command is quit or the line is null, otherwise return true.

       if(null!= line){
           currentLine = line;
           parseCommand = new StringTokenizer(line);
           String command = getNextToken();
           System.out.println(command +\" : \");
           switch(command){
           case \"createFile\":
               createFile();
               break;

           case \"printFile\":
               printFile();
               break;

           case \"lastModified\":
               lastModified();
               break;

           case \"size\":
               size();
               break;

           case \"rename\":
               rename();
               break;

           case \"mkdir\":
               mkdir();
               break;

           case \"delete\":
               delete();
               break;

           case \"list\":
               list();
               break;

           case \"quit\":
               break;


           }
           if(!\"quit\".equalsIgnoreCase(command)){
               return true;
           }
           return false;
       }

       return false;
   }

   void processCommandFile(String commandFile)
   {
       // Open up a scanner based on the commandFile file name. Read the commands from this file
       // using the Scanner line by line. For each line read, call processCommandLine. Continue reading
       // from this file as long as processCommandLine returns true and there are more lines in the file.
       // At the end, close the Scanner.

       File file = new File(commandFile);
       boolean isPrevSuccess =true;

       try {

           Scanner sc = new Scanner(file);

           while (isPrevSuccess && sc.hasNextLine()) {
               String newLine = sc.nextLine();
               isPrevSuccess = processCommandLine(newLine);
           }
           sc.close();
       }
       catch (FileNotFoundException e) {
           e.printStackTrace();
       }
   }

   public static void main(String[] args)
   {

       FileOperations fo= new FileOperations();
       for (int i=0; i < args.length; i++)
       {
           System.out.println(\"\ \ ============ Processing \" + args[i] +\" =======================\ \");
           fo.processCommandFile(args[i]);
       }

       System.out.println(\"Done with FileOperations\");
   }
}

Sample execution in Command prompt:

G:\\Command_Proj\\src>java file_operations.FileOperations G:\\ProjectSpace\\Workspace_EclipseChegg\\Chegg_Command_Proj\\cmd1
.txt


============ Processing G:\\ProjectSpace\\Workspace_EclipseChegg\\Chegg_Command_Pr
oj\\cmd1.txt =======================

? :
createFile :
file1.txt : File has been created.
printFile :
Printing the contents of the file file1.txt :
abcdefg
lastModified :
Last Modified of file1.txt is 02/06/2017 01:33:35
size :
Size in bytes of file1.txt is 7
rename :
renamed
mkdir :
Directory is created!
createFile :
dir/file2.txt : File has been created.
printFile :
Printing the contents of the file dir/file2.txt :
123456789
rename :
renamed
list :
Directory dir
File file1.txt
Directory file_operations
list :
File file2.txt
Done with FileOperations

Can someone put this code in a zip file. I tried running it last time it was answered but it did not work.. Please and thanks in advance.. In my template for ho
Can someone put this code in a zip file. I tried running it last time it was answered but it did not work.. Please and thanks in advance.. In my template for ho
Can someone put this code in a zip file. I tried running it last time it was answered but it did not work.. Please and thanks in advance.. In my template for ho
Can someone put this code in a zip file. I tried running it last time it was answered but it did not work.. Please and thanks in advance.. In my template for ho
Can someone put this code in a zip file. I tried running it last time it was answered but it did not work.. Please and thanks in advance.. In my template for ho
Can someone put this code in a zip file. I tried running it last time it was answered but it did not work.. Please and thanks in advance.. In my template for ho
Can someone put this code in a zip file. I tried running it last time it was answered but it did not work.. Please and thanks in advance.. In my template for ho

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site