package FileHandling;
import java.io.File;
import java.io.IOException;
public class DirectoryCreation {
public static void main(String[] args) throws IOException {
String strDirectory="test";
String manyDirectories="dir1/dir2/dir3";
boolean doesDirectoryExists=false;
boolean success=false;
doesDirectoryExists=new File(strDirectory).exists();
System.out.println("Is Directory Exists: "+doesDirectoryExists);
if(!doesDirectoryExists)
{
success=new File(strDirectory).mkdir();
if(success)
{
System.out.println("Directory "+strDirectory+" Created");
}
String newFile="test/newFile.txt";
File f1=new File(newFile);
success=f1.createNewFile();
System.out.println("File Created: "+success);
}
doesDirectoryExists=new File(manyDirectories).exists();
if(!doesDirectoryExists)
{
success=new File(manyDirectories).mkdirs();
if(success)
{
System.out.println("Directories "+manyDirectories+" created");
}
}
}
}
import java.io.File;
import java.io.IOException;
public class DirectoryCreation {
public static void main(String[] args) throws IOException {
String strDirectory="test";
String manyDirectories="dir1/dir2/dir3";
boolean doesDirectoryExists=false;
boolean success=false;
doesDirectoryExists=new File(strDirectory).exists();
System.out.println("Is Directory Exists: "+doesDirectoryExists);
if(!doesDirectoryExists)
{
success=new File(strDirectory).mkdir();
if(success)
{
System.out.println("Directory "+strDirectory+" Created");
}
String newFile="test/newFile.txt";
File f1=new File(newFile);
success=f1.createNewFile();
System.out.println("File Created: "+success);
}
doesDirectoryExists=new File(manyDirectories).exists();
if(!doesDirectoryExists)
{
success=new File(manyDirectories).mkdirs();
if(success)
{
System.out.println("Directories "+manyDirectories+" created");
}
}
}
}
After running this program directories will be created
0 comments:
Post a Comment