This article will teach you on how to create and delete file(s) and directory(ies).
Below are the following code:
FileCreator.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.log4j.Logger;
/**
* Utility for creating new files and directories
* @author Froilan Cardosa Benito
* @since August 25, 2007
*/
public class FileCreator {
/**
* This method is used to create file.
* @param path
* @param fileName
*/
public void createFile(String path, String fileName){
try {
OutputStream fileWriter = new FileOutputStream(path + fileName);
fileWriter.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* This method is used to create directory.
* @param directoryName
* @return boolean
*/
public boolean createDirectory(String directoryName){
boolean isSuccessful = false;
try{
File directory = new File(directoryName);
if(!directory.exists()){
isSuccessful = (new File(directoryName)).mkdir();
if (isSuccessful) {
System.out.println("\n~ Created directory " + directoryName + " successfully!");
}
}else{
// Remove the previously created directory.
deleteDir(directory);
// Create a new directory.
isSuccessful = (new File(directoryName)).mkdir();
if (isSuccessful) {
System.out.println("\n~ Created directory " + directoryName + " successfully!");
}
}
} catch(Exception e){
System.out.println(e.getMessage(), e);
isSuccessful = false;
}
return isSuccessful;
}
/**
* This method is used to delete a specific directory.
* @param dir
* @return boolean
*/
private boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
System.out.println("Unable to delete directory " + dir.getName());
return false;
}else{
System.out.println("\n~ Deleted directory " + dir.getName() + " successfully!");
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
/**
* this method is used to create directory without existing delete.
* @param directoryName
* @return boolean
*/
public boolean createDirectoryWithoutExistingDelete(String directoryName){
boolean isSuccessful = false;
try{
File directory = new File(directoryName);
if(!directory.exists()){
isSuccessful = (new File(directoryName)).mkdir();
if (isSuccessful) {
System.out.println("\n~ Created directory " + directoryName + " successfully!");
}
}else{
System.out.println("\n~ Directory " + directoryName + " exists!");
}
} catch(Exception e){
System.out.println(e.getMessage(), e);
isSuccessful = false;
}
return isSuccessful;
}
/**
* This method is used to create multiple directories.
* @param directoryNames
* @return boolean
*/
public boolean createDirectories(String directoryNames){
boolean isSuccessful = false;
try{
isSuccessful = (new File(directoryNames)).mkdirs();
if (isSuccessful) {
System.out.println("\n~ Directories: " + directoryNames + " created!");
}
} catch(Exception e){
System.out.println(e.getMessage(), e);
isSuccessful = false;
}
return isSuccessful;
}
}
Below are the following code:
FileCreator.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.log4j.Logger;
/**
* Utility for creating new files and directories
* @author Froilan Cardosa Benito
* @since August 25, 2007
*/
public class FileCreator {
/**
* This method is used to create file.
* @param path
* @param fileName
*/
public void createFile(String path, String fileName){
try {
OutputStream fileWriter = new FileOutputStream(path + fileName);
fileWriter.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* This method is used to create directory.
* @param directoryName
* @return boolean
*/
public boolean createDirectory(String directoryName){
boolean isSuccessful = false;
try{
File directory = new File(directoryName);
if(!directory.exists()){
isSuccessful = (new File(directoryName)).mkdir();
if (isSuccessful) {
System.out.println("\n~ Created directory " + directoryName + " successfully!");
}
}else{
// Remove the previously created directory.
deleteDir(directory);
// Create a new directory.
isSuccessful = (new File(directoryName)).mkdir();
if (isSuccessful) {
System.out.println("\n~ Created directory " + directoryName + " successfully!");
}
}
} catch(Exception e){
System.out.println(e.getMessage(), e);
isSuccessful = false;
}
return isSuccessful;
}
/**
* This method is used to delete a specific directory.
* @param dir
* @return boolean
*/
private boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
System.out.println("Unable to delete directory " + dir.getName());
return false;
}else{
System.out.println("\n~ Deleted directory " + dir.getName() + " successfully!");
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
/**
* this method is used to create directory without existing delete.
* @param directoryName
* @return boolean
*/
public boolean createDirectoryWithoutExistingDelete(String directoryName){
boolean isSuccessful = false;
try{
File directory = new File(directoryName);
if(!directory.exists()){
isSuccessful = (new File(directoryName)).mkdir();
if (isSuccessful) {
System.out.println("\n~ Created directory " + directoryName + " successfully!");
}
}else{
System.out.println("\n~ Directory " + directoryName + " exists!");
}
} catch(Exception e){
System.out.println(e.getMessage(), e);
isSuccessful = false;
}
return isSuccessful;
}
/**
* This method is used to create multiple directories.
* @param directoryNames
* @return boolean
*/
public boolean createDirectories(String directoryNames){
boolean isSuccessful = false;
try{
isSuccessful = (new File(directoryNames)).mkdirs();
if (isSuccessful) {
System.out.println("\n~ Directories: " + directoryNames + " created!");
}
} catch(Exception e){
System.out.println(e.getMessage(), e);
isSuccessful = false;
}
return isSuccessful;
}
}
No comments:
Post a Comment