Question: Java: Create a program that will create backup copies of a list of files. Backup.java has been partially written, complete the methods left incomplete. This
Java:
Create a program that will create backup copies of a list of files.
Backup.java has been partially written, complete the methods left incomplete. This class will be called by a program called Backup_Driver.java which tests to ensure that you have dealt with the various issues that might occur with the Backup.java class. Note: Backup.java does not have a main method. It is instantiated and used by Backup_Driver.java.
Make sure that it will function no matter what data is sent. The start() method expects an array of Strings. It is possible that the array may be null or empty. It is also possible that the array is sparsethat is, some elements are empty (or null). You need to handle these issues in the start() and backup()methods. The copyFile() method is fully functional and handles the issue of the existence or non-existence of a file to be copied.
These methods return boolean values if the method completes successfully. Since the backup() method calls the copyFile() method, there is no need to test that separately.
The buildFileArray() method will report success == true
The String array is not null and not empty, and
Each element of the String array is not null and not empty
The backup() method will report success == true if:
The File array is not null and not empty, and
Each element of the File array is not null, and
Each element of the File array does not represent an empty String (e.g., new File( "" ) ), and
All files actually exist and thus can be copied
A single element array should succeed if all of the above apply.
public class Backup {
private File[] files;
/**
* Backup each file in the array of File objects to a new file using the
* copyFile() method below. You should make sure that the array is
* neither null or empty. You should make sure also that each element of
* the array is not empty (not null). If a file cannot be copied (because
* the array element is null) then print an error message similar to
* that in the start method above.
*
* If the array is null or empty, or if any of the File objects cannot be
* copied (they don't exist, for example), the method should return false.
* If all files in the array can be copied, then return true.
*
* @param an array of File objects
* @return false if unable to copy all (or any) files in the array
*/
public boolean backup( File[] files ) {
// complete me!!
return false;
} // method backup
/**
* Build an array of File objects from the array of Strings. Return false if the
* array is null or empty or if any of the Strings cannot be used to create a
* File object (for example, new File( "" ) )
*
* @param fileNames
* @return
*/
public boolean buildFileArray( String[] fileNames)
{
// complete me
return false;
}
/**
* copies an individual file
*
* @param a single File object
* @return false if unable to copy the file
*/
public boolean copyFile( File file ) {
boolean success = false;
try
{
if ( file.exists() )
{
BufferedReader reader = new BufferedReader(
new FileReader( file ) );
File newFile = new File( file.getName() + ".bkp" );
if ( newFile.exists() )
System.out.println( "Duplicate file: " +
newFile.getName() + " will be overwritten." );
BufferedWriter writer = new BufferedWriter(
new FileWriter( newFile ) );
String line = reader.readLine();
while ( line != null )
{
writer.write( line + " " );
line = reader.readLine();
}
reader.close();
writer.close();
success = true;
} // end if
else
{
System.out.println( "Cannot copy file: " + file.getName()
+ " does not exist." );
success = false;
}
}
catch ( IOException e )
{
/* do nothing */ }
return success;
} // method copyFile
/**
* Start the program - receive an array of file names & pass an array of
* files to the backup method
*
* @param an array of filenames (String)
*/
public void start( String[] fileNames )
{
if ( fileNames != null && fileNames.length > 0 ) {
buildFileArray( fileNames );
backup( files );
} // end if
else
System.out.println( "Cannot copy. Array is null." );
} // method start
} // class Backup
Having trouble to start this program
************************************************************************
public static void main(String[] args) throws IOException
{
Backup backup = new Backup();
String[] fileNames = new String[3];
System.out.println(" Testing full array");
fileNames[0] = "DeclarationOfIndependence.rtf";
fileNames[1] = "gettysburgAddress.rtf";
fileNames[2] = "tjefferson.txt";
cleanUp(); // get rid of backup files if they exist
backup.start(fileNames);
fileNames = new String[5];
System.out.println(" Testing partial array");
fileNames[1] = "DeclarationOfIndependence.rtf";
fileNames[2] = "gettysburgAddress.rtf";
fileNames[4] = "tjefferson.txt";
cleanUp(); // get rid of backup files if they exist
backup.start(fileNames);
System.out.println(" Testing null array");
cleanUp(); // get rid of backup files if they exist
backup.start(null);
System.out.println(" Testing duplicate files");
fileNames[0] = "DeclarationOfIndependence.rtf";
fileNames[1] = "DeclarationOfIndependence.rtf";
fileNames[2] = "WarAndPeace.txt";
fileNames[3] = "WarAndPeace.txt";
fileNames[4] = "DeclarationOfIndependence.rtf";
cleanUp(); // get rid of backup files if they exist
backup.start(fileNames);
System.out.println(" Testing empty array");
fileNames = new String[0];
backup.start(fileNames);
}
public static void cleanUp()
{
File[] files = new File[ 3 ];
files[0] = new File( "DeclarationOfIndependence.rtf.bkp" );
files[1] = new File( "gettysburgAddress.rtf.bkp" );
files[2] = new File( "tjefferson.txt.bkp" );
for ( File file : files )
if ( file.exists() )
file.delete();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
