Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java Create Directory - Java Tutorial 
 

In the section of Java Tutorial you will learn how to create directory using java program.

 

Java Create Directory - Java Tutorial

                         

Introduction

In the section, you will learn how a directory or subdirectories are created. This program also explains the process of creating all non-existent ancestor directories automatically. We will use the File class to crate the directory.

 File
The File class an abstract representation of file and directory pathnames. File class is used to interact with the files system.


Here is the code for creating directory and all non-existing ancestor directories:

import java.io.*;
class CreateDirectory 
{
   public static void main(String args[])
  {
      try{
    String strDirectoy ="test";
    String strManyDirectories="dir1/dir2/dir3";

    // Create one directory
    boolean success = (new File(strDirectoy)).mkdir();
    if (success) {
      System.out.println("Directory: " + strDirectoy + " created");
    }    
  
    // Create multiple directories
    success = (new File(strManyDirectories)).mkdirs();
    if (success) {
      System.out.println("Directories: " + strManyDirectories + " created");
    }

    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}

Output of the program: 

C:\nisha>javac CreateDirectory.java

C:\nisha>java CreateDirectory
Directory: test created
Directories: dir1/dir2/dir3 created

C:\nisha>

This program takes inputs to create a directory "test" and subdirectories "dir1\dir2\dir3". the mkdir( ) method is used to create a single directory while the mkdirs( ) method is used to create multiple subdirectories.

Download the code

                         

» View all related tutorials
Related Tags: c orm form time script object io objects help method sed system ip collection this opera create show for work

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

I see in the code that you state that the new directory will be test and sub directories are dir1, dir2, dir3. How would I make it so you can name the directory in the command prompt?

Posted by BackSlash on Tuesday, 04.7.09 @ 11:47am | #86591

Hi I have a problem with my ftp, my code is not working. i may made mistake somehow, if some one can helping me pease. The code is like this.
import java.io.*;
import java.net.*;
import java.util.*;

public class Ass_2 {
public static void main(String[] args){
String currentPath = new String(); currentPath = "ftp://ftp.scit.wlv.ac.uk/pub";
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str,user,pass ;
System.out.println("\n\nTYPE HELP for view the list of commands and there Formats!!");
System.out.println("enter command 'bye' to exit the application");
System.out.print("Enter Username: ");
user = in.readLine();
System.out.print("Enter Password: ");
pass = in.readLine();
String tempPath = new String();
String toFile = new String();
tempPath = currentPath ;
readPage(currentPath);
while (!(str = in.readLine()).equals("bye")){
int tokencount;
StringTokenizer token = new StringTokenizer(str);
tokencount = token.countTokens();
String block[] = new String[tokencount];
for (int i = 0; i < tokencount; i++)
{
block[i] = token.nextToken();
System.out.println(block[i]);
}
if (block[0].equalsIgnoreCase("dir")){
System.out.println("DIR");
System.out.println(currentPath);
readPage(currentPath);
System.out.println("\n");
}else if(block[0].equalsIgnoreCase("cwd")){
System.out.println("Change Directory");
currentPath = changeDirectory(currentPath, block[1]);
System.out.println("\n");
}else if(block[0].equalsIgnoreCase("get")){
System.out.println("Wait Copying File : "+block[1]);
copyFile(currentPath,block[1]);
}else if(block[0].equalsIgnoreCase("cd..")){
currentPath = goBack(currentPath);
}else if(block[0].equalsIgnoreCase("help")){
System.out.println("\nYour in the Help Section \n");
}else {
System.out.println("Wrong Command or Format, Type Help!!!");
System.out.println("\n");
}
}
}
catch(Exception e){
System.out.println(e);
}
}

// Function to read content form the specfied path
static void readPage(String path){
String tempPath = new String();
try {
URL pageref = new URL(path);
InputStream in = pageref.openStream();
BufferedReader inline = new BufferedReader(new InputStreamReader(in));//(instr);
String line;
int linect = 0;
while((line = inline.readLine())!=null){
linect++;
System.out.println(line);
}
System.out.println("lines read = "+linect);
}
catch(Exception e){
System.out.println(e);
}
}
static String changeDirectory(String path,String dir){
path = path+"/"+dir;//System.out.println(">>>>>>>>>>> " +path+" >>> \n");
readPage(path);

return path;
}
// Function to go one setp back from the current directory
static String goBack(String currentPath){
return currentPath;
}
/// Function for basic copy TXT file
static void copyFile(String path,String file){
try {
path = path +"/"+ file; //System.out.println("Copy Path : " + path);
String fileName = new String();
fileName = file+".txt";
System.out.println("Out put file name : " + fileName);
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName));
URL pageref = new URL(path);
InputStream in = pageref.openStream();
BufferedReader inline = new BufferedReader(new InputStreamReader(in));//(instr);
String line;
int linect = 0;
while((line = inline.readLine())!=null){
System.out.println(line);
linect++; //System.out.println(line);
out.write(line+"\r\n");
}
out.close();
System.out.println("File - " +file+ " - copyed Successfully !!");
}
catch(Exception e){
System.out.println(e);
}
}
}

Posted by yare on Sunday, 01.13.08 @ 08:29am | #45091

Hi..

Thanks a lot for ur code....
it help me a lot...

I create directory in server from client machine... Its very useful for me....

How can i do the various operation(addition,subtraction,multiplication & division) in threading concept....its available in server????

plz help me with code...

thanks in advance...

Arthi...

Posted by Arthi on Friday, 11.30.07 @ 09:51am | #40958

Thanks for this code really this helps me.
can you help me creating file and directory at server machine through client machine .
plz help me
thanks in advance
garima

Posted by Garima Mishra on Wednesday, 11.28.07 @ 12:29pm | #40802

Hi..

Thanks a lot for ur code....
it help me a lot...

Can I create directory in server from client machine..????

How can i choose the various files available in server????

plz help me with code...

thanks in advance...

syam..

Posted by syam on Wednesday, 02.28.07 @ 15:53pm | #9953

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.