Question

Your software should: Ask the user for the source directory and a destination. The source is...

Your software should:

  1. Ask the user for the source directory and a destination. The source is the directory to be copied; the destination is the directory that will be the parent of the new copy.

  2. First your program should make a new directory in the new location with the same name as the source directory. (You may need to do something special for root directories if you are copying an entire disk. A root directory has no parent directory, and often, no name.)

  3. Then your program should create an array with File class objects for each item in the contents of the source directory, similar to what was done in DirectoryListDemo.

  4. Next , it should iterate the array, and for each item in the array,

    1. if it is a file, copy the file to the new directory using the copyFile() method taken from CopyFileDemoE.

    2. if it is a directory, recursively call this method to copy the directory and all of its contents.

The finished program will be a very useful utility. For example, a user could put an old flash ROM drive in one USB port, and a new flash ROM Drive in another USB port, and then, assuming that the two ports are the E: and F: drives, the user could run your program and tell it to copy E:/ to F:/, which would make a copy of the USB drive.


Java
0 0
Add a comment Improve this question Transcribed image text
Answer #1

**************************FileOperation.java************************************

package file_operations;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

/**

* The Class FileOperation provides the implementation of creating new

* directories and copy content from older to new directories.

*/

public class FileOperation {

/**

* The file directories. This variable holds the destination location and

* src file.

*/

private Map<String, File[]> fileDirectories;

/**

* Instantiates a new file operation.

*/

public FileOperation() {

fileDirectories = new HashMap<>();

}

/**

* Make new directories. This method is used to create new directories with

* same structure as in older one.

*

* @param source the source

* @param destination the destination

*/

public void makeNewDirectories(String source, String destination) {

File[] directories = new File(source).listFiles();

for (File dir : directories) {

String src = dir.getAbsolutePath();

String dest = destination + src.substring(src.indexOf(":") + 1, src.length());

new File(dest).mkdirs();

File[] filenames = new File(src).listFiles();

fileDirectories.put(dest, filenames);

}

}

/**

* Copy file. This method is used to copy content from older file location

* to new file loaction

* @throws IOException Signals that an I/O exception has occurred.

*/

public void copyFile() throws IOException {

Set<Entry<String, File[]>> entry = fileDirectories.entrySet();

FileReader fr = null;

FileWriter fw = null;

BufferedWriter bw = null;

BufferedReader br = null;

try {

Iterator<Entry<String, File[]>> itr = entry.iterator();

while (itr.hasNext()) {

String key = itr.next().getKey();

File[] fileList = fileDirectories.get(key);

// This for loop is implemented to copy content from source to

// destination file location.

for (File file : fileList) {

String destFile = key + File.separator + file.getName();

fr = new FileReader(file);

fw = new FileWriter(destFile);

br = new BufferedReader(fr);

bw = new BufferedWriter(fw);

String line = null;

while ((line = br.readLine()) != null) {

bw.write(line);

bw.newLine();

}

bw.flush();

}

}

}

catch (IOException e) {

System.out.println(e);

}

finally {

fr.close();

fw.close();

}

}

}

********************************FileOperator.java************************************

package file_operations;

import java.io.IOException;

import java.util.Scanner;

/**

* The Class FileOperator provides implementation to take user input i.e.

* location directory locations.

*/

public class FileOperator {

/** The scan. */

private static Scanner scan = new Scanner(System.in);

/**

* The main method.

*

* @param args the arguments

* @throws IOException Signals that an I/O exception has occurred.

*/

public static void main(String[] args) throws IOException {

System.out.println("Enter source location: ");

String source = scan.nextLine();

System.out.println("Enter destination location: ");

String destination = scan.nextLine();

FileOperation fo = new FileOperation();

// first new directories are created

fo.makeNewDirectories(source, destination);

// Copying file to new location

fo.copyFile();

System.out.println("********Finished************");

}

}

********************Output*****************************

e Console X Tasks of* JUnit Gradle Executions <terminated> FileOperator (Java Application] C:\Program Files\Java\jre1.8.0_121

**************************************************************************************************************************
Please rate the solution to improve our solution standard. Please comment if you have any query. Thanks

Add a comment
Know the answer?
Add Answer to:
Your software should: Ask the user for the source directory and a destination. The source is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A File class object may refer to a data file or a directory. The IntelliJ project...

    A File class object may refer to a data file or a directory. The IntelliJ project Directory List Demo, discussed starting on page 8, shows how to get information about the contents of a directory using File class methods list() and listFle(). The IntelliJ project Create DirectoriesDemo, starting on page 10, shows how to make a set of directories using the File class method mkdir(). The IntelliJ project CopyFileDemoE has a method to copy a file. Your task is to...

  • Really need help from 11 on: Create the directory structure IFT383FinalExam/Activities/Activity1 in your home directory. Using...

    Really need help from 11 on: Create the directory structure IFT383FinalExam/Activities/Activity1 in your home directory. Using the cat command, create a file named classRoster with the following fields, separated by a comma. Student ID First Name Last Name Grade Program of Study ASURITE ID (username) Add three records to your file. Display the contents of the file. Move the file classRoster to the directory Activity1. Go to the Activity1 directory. Display the directory you are in. Add read, write and...

  • With your partner, brainstorm a program that will ask the user for a number of digits...

    With your partner, brainstorm a program that will ask the user for a number of digits to be stored. The program should then create an array of that size and then prompt the user for numbers to fill each position in the array. When all of the positions are filled, display the contents of the array to the user. Create a new Project named FillArray and a new class in the default packaged named FillArray.java. You and your partner should...

  • do numbers 4-8 4. Given any directory, use the Is command to display: • all files...

    do numbers 4-8 4. Given any directory, use the Is command to display: • all files and sub-directories starting with the letter "D" (note do not list anything in any sub-directory) • its immediate sub-directories (sub-directories only, and no other ordinary files) its immediate hidden sub-directories only - take a screenshot (#3-3) that clearly shows the command and the result. 5. Assume that the following files are in the working directory: $ ls intro notesb ref2 section 1 section3 section4b...

  • Select your answer from here. absolute pathname to the file called xyz changes the directory to...

    Select your answer from here. absolute pathname to the file called xyz changes the directory to the parent of the current directory. lists current directory files including the invisible files sends xyz file to the line printer deletes the directory called xyz displays the content of the file called xyz displays the current directory pathname cancels the printing job on the 1p 1 printer confirms the deletion of the xyz file before deleting it lists the current directory in long...

  • Write a bash script to protect a directory. Your script should notify a user of the...

    Write a bash script to protect a directory. Your script should notify a user of the following: -New file was created -Permission changes -Ownership changes -File size changes -Modification date changes Your script should have two operation modes, protect and check. Use command line flags, -p <directory> and -c <directory> to decide which mode to operate in. In protect mode, your script should create a file called .protect inside the directory you want to watch. You will use this file...

  • How can I create Loops and Files on Java? • Create a program that reads a...

    How can I create Loops and Files on Java? • Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...

  • c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments:...

    c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to the file, and then close the file. Write another function named fileToArray. This function should accept three argu- ments: the name of a file, a pointer to an int array, and the size...

  • 1. Navigate to your home directory by typing cd. Now type ls and notice that there...

    1. Navigate to your home directory by typing cd. Now type ls and notice that there is a subdirectory called Downloads. Now navigate to the root of the file system by typing cd /. From here, which single command would you use to navigate directly to the Downloads folder that is located in your home directory? The output of the command would be as follows: 2. From the Downloads directory which single command would you use to navigate back up...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT