安静模式删除目录,操作过程中会抛出异常deleteQuietly(File file)
publicstatic boolean deleteQuietly(File file) {
if (file == null) {
return false;
}
try {
if (file.isDirectory()) {
cleanDirectory(file);
}
} catch (Exception ignored) {
}
try {
return file.delete();
} catch (Exception ignored) {
return false;
}
}
清除一个目录而不删除它cleanDirectory(Filedirectory)
publicstatic void cleanDirectory(File directory) throws IOException {
if (!directory.exists()) {
String message = directory +” does not exist”;
throw newIllegalArgumentException(message);
}
if (!directory.isDirectory()) {
String message = directory +" is not a directory";
throw newIllegalArgumentException(message);
}
File[] files =directory.listFiles();
if (files == null) { // null if security restricted
throw newIOException("Failed to list contents of " + directory);
}
IOException exception = null;
for (File file : files) {
try {
forceDelete(file);
} catch (IOException ioe) {
exception = ioe;
}
}
if (null != exception) {
throw exception;
}
}
等待NFS来传播一个文件的创建,实施超时waitFor(File file, int seconds)
publicstatic boolean waitFor(File file, int seconds) {
int timeout = 0;
int tick = 0;
while (!file.exists()) {
if (tick++ >= 10) {
tick = 0;
if (timeout++ > seconds){
return false;
}
}
try {
Thread.sleep(100);
} catch (InterruptedExceptionignore) {
// ignore exception
} catch (Exception ex) {
break;
}
}
return true;
}
把一个文件的内容读取到一个对应编码的字符串中去readFileToString(File file, String encoding)
public static String readFileToString(Filefile, String encoding) throws IOException {
InputStream in = null;
try {
in = openInputStream(file);
return IOUtils.toString(in,encoding);
} finally {
IOUtils.closeQuietly(in);
}
}
读取文件的内容到虚拟机的默认编码字符串readFileToString(File file)
public static String readFileToString(Filefile) throws IOException {
return readFileToString(file,null);
}
把一个文件转换成字节数组返回readFileToByteArray(File file)
public static byte[] readFileToByteArray(Filefile) throws IOException {
InputStream in = null;
try {
in = openInputStream(file);
return IOUtils.toByteArray(in);
} finally {
IOUtils.closeQuietly(in);
}
}
把文件中的内容逐行的拷贝到一个对应编码的list中去
publicstatic List readLines(File file, String encoding) throwsIOException {
InputStream in = null;
try {
in = openInputStream(file);
return IOUtils.readLines(in,encoding);
} finally {
IOUtils.closeQuietly(in);
}
}
把文件中的内容逐行的拷贝到一个虚拟机默认编码的list中去
public static ListreadLines(File file) throws IOException {
return readLines(file, null);
}
根据对应编码返回对应文件内容的行迭代器lineIterator(File file, String encoding)
publicstatic LineIterator lineIterator(File file, String encoding) throws IOException{
InputStream in = null;
try {
in = openInputStream(file);
return IOUtils.lineIterator(in,encoding);
} catch (IOException ex) {
IOUtils.closeQuietly(in);
throw ex;
} catch (RuntimeException ex) {
IOUtils.closeQuietly(in);
throw ex;
}
}
根据虚拟机默认编码返回对应文件内容的行迭代器lineIterator(File file)
public static LineIterator lineIterator(Filefile) throws IOException {
return lineIterator(file, null);
}
根据对应编码把字符串写进对应的文件中writeStringToFile(File file, String data, String encoding)
publicstatic void writeStringToFile(File file, String data, String encoding) throwsIOException {
OutputStream out = null;
try {
out = openOutputStream(file);
IOUtils.write(data, out,encoding);
} finally {
IOUtils.closeQuietly(out);
}
}
根据虚拟机默认编码把字符串写进对应的文件中writeStringToFile(File file, String data)
public static void writeStringToFile(Filefile, String data) throws IOException {
writeStringToFile(file, data,null);
}
根据虚拟机默认的编码把CharSequence写入到文件中(File file, CharSequence data)
public static void write(File file, CharSequence data) throwsIOException {
String str = data == null ? null :data.toString();
writeStringToFile(file, str);
}
根据对应的编码把CharSequence写入到文件中write(File file, CharSequence data, String encoding)
publicstatic void write(File file, CharSequence data, String encoding) throwsIOException {
String str = data == null ? null :data.toString();
writeStringToFile(file, str,encoding);
}
把一个字节数组写入到指定的文件中writeByteArrayToFile(File file, byte[] data)
public static void writeByteArrayToFile(Filefile, byte[] data) throws IOException {
OutputStream out = null;
try {
out = openOutputStream(file);
out.write(data);
} finally {
IOUtils.closeQuietly(out);
}
}
把集合中的内容根据对应编码逐项插入到文件中writeLines(File file, String encoding, Collection