Question

A File class object may refer to a data file or a directory. The IntelliJ project Directory List Demo, discussed starting on
3. Then your program should create an array with File class objects for each item in the contents of the source directory, si
You should submit the IntelliJ project folder with your software.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Create Simple java project in intellij -> create class with name "CopyFiles" -> paste the code and run.

Make comment if you face any difficulty or you need any modification. Thanks

import java.io.File;

import java.nio.file.Files;

import java.nio.file.StandardCopyOption;

import java.util.Scanner;

public class CopyFiles {

public void copy(File source, File destination) throws Exception {

if (!source.exists()) {

throw new Exception("Source file or directory path does not exists.");

}

if (!destination.exists()) {

destination.mkdirs();

System.out.println("Directory created : " + destination);

}

if (source.isDirectory()) {

File[] files = source.listFiles();

for (File file : files) {

File src = new File(source, file.getName());

File dest = new File(destination, file.getName());

System.out.println("Src : " + src);

System.out.println("Dest : " + dest);

// Recursive function call

copy(src, dest);

}

} else {

Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);

System.out.println("File copied :: " + destination);

}

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Please provide source directory name : ");

String srcPath = sc.next();

System.out.println("Please provide destination directory name : ");

String destPath = sc.next();

File source = new File(srcPath);

File destination = new File(destPath, source.getName());

System.out.println(source.exists() + " : " + source.getPath());

System.out.println(destination.exists() + " : " + destination.getPath());

CopyFiles object = new CopyFiles();

try {

object.copy(source, destination);

} catch (Exception e) {

e.printStackTrace();

}

}

}

Add a comment
Know the answer?
Add Answer to:
A File class object may refer to a data file or a directory. The IntelliJ project...
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
  • Your software should: Ask the user for the source directory and a destination. The source is...

    Your software should: 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. 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...

  • Question: Hands-On Project 3-3 Use a Batch File A file with a bat file extension is...

    Question: Hands-On Project 3-3 Use a Batch File A file with a bat file extension is called a batch file. Yo... Use a Batch File A file with a .bat file extension is called a batch file. You can use a batch file to execute a group of commands, sometimes called a script, from a command prompt. Do the following to learn to use a batch file: 1. Using a command prompt window, copy the files in your Documents folder...

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

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • JAVA 1336 Problem: Complete Programming Project 1 (pg. 339, Savitch). The defined class will be HotDogStand.java...

    JAVA 1336 Problem: Complete Programming Project 1 (pg. 339, Savitch). The defined class will be HotDogStand.java and will also contain: • A default constructor • An overloaded constructors Getters and Setters A 'copy' constructor, -3 A 'toString' method, An 'equals' method, A finalize' method, Remember to THOROUGHLY test the application in a separate test file, HotDogStandTest.java. Requirements: You will submit the following with this lab (face-to-face sections). Online sections will turn their labs in through Blackboard as usual: a. The...

  • Creating a Shell Interface Using Java This project consists of modifying a Java program so that...

    Creating a Shell Interface Using Java This project consists of modifying a Java program so that it serves as a shell interface that accepts user commands and then executes each command in a separate process external to the Java virtual machine. Overview A shell interface provides the user with a prompt, after which the user enters the next command. The example below illustrates the prompt jsh> and the user’s next command: cat Prog.java. This command displays the file Prog.java on...

  • Information About This Project             In the realm of database processing, a flat file is a...

    Information About This Project             In the realm of database processing, a flat file is a text file that holds a table of records.             Here is the data file that is used in this project. The data is converted to comma    separated values ( CSV ) to allow easy reading into an array.                         Table: Consultants ID LName Fee Specialty 101 Roberts 3500 Media 102 Peters 2700 Accounting 103 Paul 1600 Media 104 Michael 2300 Web Design...

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

  • Create a New Java Project called YourLastNameUpperCase. Write a program that asks the user for the...

    Create a New Java Project called YourLastNameUpperCase. Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be opened for writing. The program should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, except that all the characters will be uppercase. Use...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

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