Java FileHandler

package com.karl.filehandler;

publicclass FileProxyFactory {

private static FileProxy instance;

static {

init();

}

private static void init() {

if (instance == null) {

System.out.println("abc");

instance = new FileInJdk();

}

}

public static FileProxy getFileProxy() {

return instance;

}

}

package com.karl.filehandler;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public interface FileProxy {

/**

* Tests whether the file or directory denoted by this abstract pathname exists.

*/

boolean exists(String file);

/**

* delete file

*/

boolean deleteFile(String file);

/**

* delete directory

*/

boolean deleteDirectory(String directory);

/**

* change the file mode

*/

boolean giveFullPermission(String filePath);

/**

* Creates the directory named by this abstract pathname, including any necessary

* but nonexistent parent directories.

*

* Note that if this operation fails it may have succeeded in creating some of

* the necessary parent directories.

*/

boolean mkdirs(String dirs);

/**

* Creates a new, empty file named by this filename if and only if a file with this name does not yet exist.

*/

boolean createNewFile(String filename);

/**

* Get file input stream for reading file content.

*/

InputStream getFileInputStream(String filename) throws IOException;

/**

* Get file output stream for writing file content.

*/

OutputStream getFileOutputStream(String filename) throws IOException;

}

package com.karl.filehandler;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public class FileInJdk implements FileProxy {

@Override

public boolean exists(String file) {

File f = new File(file);

return f.exists();

}

@Override

public boolean deleteFile(String filePath) {

boolean isOk = false;

File file = new File(filePath);

// if file exists, delete it

if (file.isFile() && file.exists()) {

file.delete();

isOk = true;

}

return isOk;

}

@Override

public boolean giveFullPermission(String filePath) {

File file = new File(filePath);

if (file.exists()) {

try {

Runtime.getRuntime().exec("chmod 777 " + filePath);

} catch (Exception ex) {

System.out.println("Failed to set full permission for file("

+ filePath + ").");

System.out.println("FileInJdk:giveFullPermission" + ex);

return false;

}

return true;

}

return false;

}

@Override

public boolean deleteDirectory(String directoryPath) {

boolean isOk = false;

// if directoryPath not end with file separator, add it automatically

if (!directoryPath.endsWith(File.separator)) {

directoryPath = directoryPath + File.separator;

}

File file = new File(directoryPath);

// if not exist or not a directory, then exit

if (!file.exists() || !file.isDirectory()) {

return isOk;

}

isOk = true;

// delete all files including sub directories

File[] files = file.listFiles();

for (int i = 0; i < files.length; i++) {

if (files[i].isFile()) {

// delete files

isOk = deleteFile(files[i].getAbsolutePath());

if (!isOk) {

break;

}

} else {

// delete sub directories

isOk = deleteDirectory(files[i].getAbsolutePath());

if (!isOk) {

break;

}

}

}

if (!isOk) {

return false;

}

// delete current directory

if (file.delete()) {

return true;

} else {

return false;

}

}

@Override

public boolean mkdirs(String dirs) {

File f = new File(dirs);

return f.mkdirs();

}

@Override

public boolean createNewFile(String filename) {

File f = new File(filename);

try {

return f.createNewFile();

} catch (Exception e) {

System.out.println("Failed to create file(" + filename + ").");

System.out.println("FileInJdk:createNewFile" + e);

return false;

}

}

@Override

public InputStream getFileInputStream(String filename) throws IOException {

return new FileInputStream(filename);

}

@Override

public OutputStream getFileOutputStream(String filename) throws IOException {

return new FileOutputStream(filename);

}

}

package com.karl.filehandler;

import junit.framework.Assert;

import com.karl.filehandler.FileProxyFactory;

import org.junit.Test;

public class TestFileHandler {

private String filePath = "c:\\content\\1.file";

@Test

public void testFileExist(){

boolean actual = FileProxyFactory.getFileProxy().exists(filePath);

boolean expected = false;

Assert.assertEquals(expected, actual);

}

@Test

public void testFileCreate(){

boolean actual = FileProxyFactory.getFileProxy().createNewFile(filePath);

boolean expected = true;

FileProxyFactory.getFileProxy().deleteFile(filePath);

Assert.assertEquals(expected, actual);

}

@Test

public void testFileDelete(){

FileProxyFactory.getFileProxy().createNewFile(filePath);

boolean actual = FileProxyFactory.getFileProxy().deleteFile(filePath);

boolean expected = true;

Assert.assertEquals(expected, actual);

}

}