Sunday, June 18, 2017

Copy Command - Java Tutorial

The entire process from preparation to structured design and from procedural programming to object oriented programming.

The missing tutorial that will show you not just what and how, but the most crucial steps of why.

Watch the video here - https://youtu.be/JM5MZxKKMgM

Plan:
Know a simple copy command from the command line and think about the process of copying a file content.  Think about the characteristics of a file and what aspects of those characteristics can be used in order to perform and to control the copy process.
Type copy /? on your terminal to read its help file and copy simple files, but make sure to make mistakes like wrong input file or wrong path to see how the utility handles those mistakes.

You can always display the return value by typing echo %errorlevel% to learn about how the programmers decided to handle error conditions.  In our case, we'll return 2017 if the input file does not exists and 2018 if the output file already exists, since we do not want to overwrite it.


Design: UML+Structured logic ( DIA )
Code: (BlueJ )

import java.io.File;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.PrintWriter;

/**
 * <h1>Copies a text file to another</h1>
 * @author Zoltan Szabo
 * @version 1.0.0
 */

public class Copy{
   private String from;
   private String to;
   /**
    * Initializes the source and destination file names
    * @param source Name of the input file
    * @param destination Name of the output file
    */

   public Copy(String source, String destination){
       from= source;
       to=destination;
    }
   
/**
 * Performs the actual file copy
 * @throws IOException if the input file does not exists
 */

public void copy()throws java.io.IOException{
   //Creates a new File instance by converting the given pathname string into an abstract pathname.
   File input=new File(from);
   File output=new File(to);
    
      //Tests whether the file or directory denoted by this abstract pathname exists.
   if(!input.exists()){
       //getName()-Returns the name of the file or directory denoted by this abstract pathname.
       System.out.println("Input file "+input.getName()+" does not exists or path is wrong!");
       //Terminates the currently running Java Virtual Machine.
       System.exit(2017);
      
    }
  
          //Tests whether the file or directory denoted by this abstract pathname exists.
   if(output.exists()){
       //getName()-Returns the name of the file or directory denoted by this abstract pathname.
       System.out.println("Output file "+output.getName()+" already exists!");
       //Terminates the currently running Java Virtual Machine.
       System.exit(2018);
      
    }
   
   //Constructs a new Scanner that produces values scanned from the specified file.
   Scanner inputFile=new Scanner(input);
   //FileWriter out=new FileWriter(output,true);
   PrintWriter outputFile=new PrintWriter(output);
  
   //Loop through the inputFile and write to outputFile - the actual work of the application
   //hasNext() - Returns true if this scanner has another token in its input.

   while(inputFile.hasNext()){
       //println - Prints a String and then terminates the line.
       //nextLine - Advances this scanner past the current line and returns the input that was skipped.

       outputFile.println(inputFile.nextLine());
    }
  
   //Closes the stream and releases any system resources associated with it.
   inputFile.close();
   outputFile.close();
}
}



/**
 *<h1> Application to copy a text file into another</h1>
 *
 * @author Zoltan Szabo
 * @version 1.0.0
 */

public class MyCopy{
    /**
     * Entry point to the application
     * @param args String array from the command line
     * @throws IOException if the input file does not exists
     */

    public static void main(String args[]) throws java.io.IOException{
       Copy myCopy=new Copy(args[0], args[1]);
       myCopy.copy();
}
}

No comments:

Post a Comment