Last active
September 25, 2017 13:14
-
-
Save javamultiplex/0d4af799ffe2c12049895e2e8afdaa1f to your computer and use it in GitHub Desktop.
How to create folder inside folder in Java?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.filehandling; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.InputMismatchException; | |
import java.util.Scanner; | |
/** | |
* | |
* @author Rohit Agarwal | |
* @category File Handling | |
* @problem How to create a directory inside directory? | |
* | |
*/ | |
public class CreateDirectoryInsideDirectory { | |
public static void main(String[] args) throws IOException { | |
Scanner input = null; | |
BufferedReader br = null; | |
try { | |
int result = 0; | |
input = new Scanner(System.in); | |
br = new BufferedReader(new InputStreamReader(System.in)); | |
System.out.println("Enter drive letter : "); | |
char ch = input.next(".").charAt(0); | |
System.out.println("Enter directory name : "); | |
String directoryName = br.readLine(); | |
System.out.println("Enter new directory : "); | |
String newDirectory = br.readLine(); | |
result = createNewDirectory(ch, directoryName, newDirectory); | |
if (result == 1) { | |
System.out.println("Directory " + newDirectory + " successfully created in directory " + ch + ":\\" + directoryName); | |
} else if (result == 0) { | |
System.out.println("Error occured, path doesn't exist."); | |
} | |
} catch (InputMismatchException e) { | |
System.out.println("Enter drive letter correctly!"); | |
} finally { | |
if (input != null) { | |
input.close(); | |
} | |
if (br != null) { | |
br.close(); | |
} | |
} | |
} | |
private static int createNewDirectory(char ch, String directoryName, String newDirectoryName) { | |
// Creating File reference. | |
File directory = new File(ch + ":\\" + directoryName); | |
// If directory doesn't exist then creating a new directory. | |
if (!directory.exists()) { | |
directory.mkdir(); | |
} | |
// Creating File reference. | |
File file = new File(directory, newDirectoryName); | |
boolean result = false; | |
int flag = 0; | |
// Checking whether directory exist or not in given path. | |
if (file.exists()) { | |
System.out.println("Directory " + newDirectoryName + " already exist in directory " + ch + ":\\" + directoryName); | |
flag = 2; | |
} else { | |
result = file.mkdir(); | |
if (result) { | |
flag = 1; | |
} | |
} | |
return flag; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment