Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A File class object may refer to a data file or a directory. The Netbeans projec

ID: 3919603 • Letter: A

Question

A File class object may refer to a data file or a directory. The Netbeans project DirectoryListDemo, discussed starting on page 8, shows how to get information about the contents of a directory using File class methods list() and listFIle(). The Netbeans projectCreateDirectoriesDemo, starting on page 10, shows how to make a set of directories using the File class method mkdir(). The NetBeans project CopyFileDemoE has a method to copy a file.

Your task is to use techniques from all three of these examples to create software that can be used to copy a directory and all of its contents, including subdirectories and their contents.  

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,

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

- 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.

PLEASE USE THIS TO GET STARTED

There will still be a few kinks to workout. However, the algorithm describes a recursive approach



Main method
? Get the source directory or file from the user
? get destination directory or file from user
? Call checkDirOrFile method and pass the (source, destination) directories.

checkDirOrFile method
? Receives files of (source, destination) directories.
? String[] array to hold the list of the directories and files
? Create an array of File objects to represent every files and directories inside the source directory.
? If directory –
• make a directory at target
• call checkDirOrFile method
? If a file -
? call a copyFile method.

copyFile method (source, destination)
? Receives files of (source, destination) directories.
? Use FileInputStream and FileOutputStream
? Use BufferedInputStream and BufferedOutputStream
? Copy bits from target to destination

Explanation / Answer

Below is the code for your requirement. Please comment if you face any difficulty.

Not able to attach ouput screenshot due to some technical issue. Please comment if you want it..

Code:

=====

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Scanner;

/**

*

* @author Expert

*

*/

public class CopyFileDemoE {

static Scanner sc = new Scanner(System.in);

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

System.out.print("Enter source path: ");

String source = sc.nextLine();

System.out.print("Enter destination path: ");

String destination = sc.nextLine();

checkDirOrFile(source,destination);

}

/**

*

* @param source

* @param destination

* @throws IOException

*/

private static void checkDirOrFile(String source, String destination) throws IOException {

File path = new File(source);

File list[] = path.listFiles();

new File(destination).mkdir();

for(int i=0; i<list.length; i++) {

if(list[i].isDirectory()) {

new File(destination+"\"+list[i].getName()).mkdir();

}

else {

copyFile(source+"\"+list[i].getName(),destination+"\"+list[i].getName());

}

}

}

/**

*

* @param source

* @param destination

* @throws IOException

*/

private static void copyFile(String source, String destination) throws IOException {

FileInputStream fin = new FileInputStream(source);

BufferedInputStream bin = new BufferedInputStream(fin);

FileOutputStream fout = new FileOutputStream(destination);

BufferedOutputStream bout = new BufferedOutputStream(fout);

bout.write(bin.read());

bout.close();

bin.close();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote