Created
May 18, 2010 20:44
-
-
Save pk11/405510 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
/** | |
* simply utility to deal with files, it's depending on IOUtilities | |
* | |
*/ | |
public class FileUtilities { | |
// charsetName can be null to use the default charset. | |
public static String readFileAsString(String fileName, String charsetName) | |
throws java.io.IOException { | |
java.io.InputStream is = new java.io.FileInputStream(fileName); | |
try { | |
final int bufsize = 4096; | |
int available = is.available(); | |
byte data[] = new byte[available < bufsize ? bufsize : available]; | |
int used = 0; | |
while (true) { | |
if (data.length - used < bufsize) { | |
byte newData[] = new byte[data.length << 1]; | |
System.arraycopy(data, 0, newData, 0, used); | |
data = newData; | |
} | |
int got = is.read(data, used, data.length - used); | |
if (got <= 0) break; | |
used += got; | |
} | |
return charsetName != null ? new String(data, 0, used, charsetName) | |
: new String(data, 0, used); | |
} finally { | |
IOUtilities.closeStream(is); | |
} | |
} | |
static public boolean deleteDirectory(File path) { | |
if( path.exists() ) { | |
File[] files = path.listFiles(); | |
for(int i=0; i<files.length; i++) { | |
if(files[i].isDirectory()) { | |
deleteDirectory(files[i]); | |
} | |
else { | |
files[i].delete(); | |
} | |
} | |
} | |
return( path.delete() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment