Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

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

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

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

Latest Searches:
sitmesh
struct
login.jsp
define functions in js
EchoPoint
jsf comments
Create a 3D bar chart
FCKediter
enumerations
Web Service applicatio
Java: Sets
Insert Data into Datab
retrieve blob field in
foreach
swing components
how to installmcat
compare two arraylist
jboss portal
Two Dimensional Array
JCalendar
3d max
Deitels
calling one jsp p
Filtering Selection fr
jsp Insert data in mys
Use of <fn:substringBe
poc dojo container
tree
java string
a comple progrma using
Photoshop Brushes Spar
jsp bean get property
url actions tag
delete data in mysql d
J2ME List Image
array in javascript
flex
dojo tree
regex progam to find v
enterprise portal
server side javascript
JMF
HSSF clone cell
chicken
DOJO
java cancel button
iBatis Stored Procedur
Ajax Login Example
ASP.NET .NET File Uplo
.setDocument(new
jndi
remote connect with js
Oracle forms
java write file line b
http servlet
Passing Parameters in
Photoshop Brushes The
3DS MAX Effects Cell S
How to use string vali
send parameters to ano
data grid insert
Photoshop Effects Grun
Use of <fn:substringBe
Eclipse SDE
sqlserver2000
count the number of di
Filter
Using Design Patterns
validate radio button
netbeans jsp
Struts1.3 tutorials do
ip address programming
Visual Basic Networkin
Javascript Randomizing
Flash Animation Tweeni
jfreechart
Applet
ghdfgsdf
iterator
javascript connect db
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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