The project CreateDirectoriesDemo is included with the files for this chapter as a zipped file. rewrite the program so that it asks the user for the location where the new directories are to be created, and then asks the user to enter, one at a time, the relative path names of the directories it should create.
Amended additional details to the above abstraction of the requirements.
package createdirectoriesdemo;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class CreateDirectoriesDemo {
public static void main(String[] args) {
// Establish the location of the parent for the new set of directories.
// This could be changed to user input.
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter directory path: ");
String s = reader.nextLine(); // Scans the next token of the input as an int.
//once finished
String[] folderPaths = new String[7];
String location = s;
for (int i = 0; i < 7; i++) {
System.out.println("Enter relative path: ");
folderPaths[i] = reader.nextLine();
}
// create a String array of the directories to be created
reader.close();
// create a File class array for directories to be created
File[] newFolders = new File[folderPaths.length];
// create new directories based on the file names in the array
for (int i = 0 ; i < newFolders.length; i++)
{
// create a File object for this new directory
// based on the parent location and each new path name
newFolders[i] = new File( location + folderPaths[i] ) ;
// make the new directory
newFolders[i].mkdir();
} // end for
} // end main()
} // end class CreateDirectoriesDemo
Get Answers For Free
Most questions answered within 1 hours.