fileutils java_Java File类总结和FileUtils类

/** Licensed to the Apache Software Foundation (ASF) under one or more

* contributor license agreements. See the NOTICE file distributed with

* this work for additional information regarding copyright ownership.

* The ASF licenses this file to You under the Apache License, Version 2.0

* (the "License"); you may not use this file except in compliance with

* the License. You may obtain a copy of the License at

*

*http://www.apache.org/licenses/LICENSE-2.0*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.*/

packagecom.mengdd.file;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;/** FileUtils copied from org.apache.commons.io.FileUtils*/

public classFileUtils {/*** Construct a file from the set of name elements.

*

*@paramdirectory

* the parent directory

*@paramnames

* the name elements

*@returnthe file*/

public staticFile getFile(File directory, String... names) {if (directory == null) {throw newNullPointerException("directorydirectory must not be null");

}if (names == null) {throw new NullPointerException("names must not be null");

}

File file=directory;for(String name : names) {

file= newFile(file, name);

}returnfile;

}/*** Construct a file from the set of name elements.

*

*@paramnames

* the name elements

*@returnthe file*/

public staticFile getFile(String... names) {if (names == null) {throw new NullPointerException("names must not be null");

}

File file= null;for(String name : names) {if (file == null) {

file= newFile(name);

}else{

file= newFile(file, name);

}

}returnfile;

}/*** Opens a {@linkFileInputStream} for the specified file, providing better

* error messages than simply calling new FileInputStream(file)

* .

*

* At the end of the method either the stream will be successfully opened,

* or an exception will have been thrown.

*

* An exception is thrown if the file does not exist. An exception is thrown

* if the file object exists but is a directory. An exception is thrown if

* the file exists but cannot be read.

*

*@paramfile

* the file to open for input, must not be {@codenull}

*@returna new {@linkFileInputStream} for the specified file

*@throwsFileNotFoundException

* if the file does not exist

*@throwsIOException

* if the file object is a directory

*@throwsIOException

* if the file cannot be read*/

public static FileInputStream openInputStream(File file) throwsIOException {if(file.exists()) {if(file.isDirectory()) {throw new IOException("File '" +file+ "' exists but is a directory");

}if (file.canRead() == false) {throw new IOException("File '" + file + "' cannot be read");

}

}else{throw new FileNotFoundException("File '" +file+ "' does not exist");

}return newFileInputStream(file);

}/*** Opens a {@linkFileOutputStream} for the specified file, checking and

* creating the parent directory if it does not exist.

*

* At the end of the method either the stream will be successfully opened,

* or an exception will have been thrown.

*

* The parent directory will be created if it does not exist. The file will

* be created if it does not exist. An exception is thrown if the file

* object exists but is a directory. An exception is thrown if the file

* exists but cannot be written to. An exception is thrown if the parent

* directory cannot be created.

*

*@paramfile

* the file to open for output, must not be {@codenull}

*@paramappend

* if {@codetrue}, then bytes will be added to the

* end of the file rather than overwriting

*@returna new {@linkFileOutputStream} for the specified file

*@throwsIOException

* if the file object is a directory

*@throwsIOException

* if the file cannot be written to

*@throwsIOException

* if a parent directory needs creating but that fails*/

public static FileOutputStream openOutputStream(File file, booleanappend)throwsIOException {if(file.exists()) {if(file.isDirectory()) {throw new IOException("File '" +file+ "' exists but is a directory");

}if (file.canWrite() == false) {throw new IOException("File '" +file+ "' cannot be written to");

}

}else{

File parent=file.getParentFile();if (parent != null) {if (!parent.mkdirs() && !parent.isDirectory()) {throw new IOException("Directory '" +parent+ "' could not be created");

}

}

}return newFileOutputStream(file, append);

}public staticFileOutputStream openOutputStream(File file)throwsIOException {return openOutputStream(file, false);

}/*** Cleans a directory without deleting it.

*

*@paramdirectory

* directory to clean

*@throwsIOException

* in case cleaning is unsuccessful*/

public static void cleanDirectory(File directory) throwsIOException {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 new IOException("Failed to list contents of " +directory);

}

IOException exception= null;for(File file : files) {try{

forceDelete(file);

}catch(IOException ioe) {

exception=ioe;

}

}if (null !=exception) {throwexception;

}

}//-----------------------------------------------------------------------

/*** Deletes a directory recursively.

*

*@paramdirectory

* directory to delete

*@throwsIOException

* in case deletion is unsuccessful*/

public static void deleteDirectory(File directory) throwsIOException {if (!directory.exists()) {return;

}

cleanDirectory(directory);if (!directory.delete()) {

String message= "Unable to delete directory " + directory + ".";throw newIOException(message);

}

}/*** Deletes a file. If file is a directory, delete it and all

* sub-directories.

*

* The difference between File.delete() and this method are:

*

*

A directory to be deleted does not have to be empty.

*

You get exceptions when a file or directory cannot be deleted.

* (java.io.File methods returns a boolean)

*

*

*@paramfile

* file or directory to delete, must not be {@codenull}

*@throwsNullPointerException

* if the directory is {@codenull}

*@throwsFileNotFoundException

* if the file was not found

*@throwsIOException

* in case deletion is unsuccessful*/

public static void forceDelete(File file) throwsIOException {if(file.isDirectory()) {

deleteDirectory(file);

}else{boolean filePresent =file.exists();if (!file.delete()) {if (!filePresent) {throw new FileNotFoundException("File does not exist: "

+file);

}

String message= "Unable to delete file: " +file;throw newIOException(message);

}

}

}/*** Deletes a file, never throwing an exception. If file is a directory,

* delete it and all sub-directories.

*

* The difference between File.delete() and this method are:

*

*

A directory to be deleted does not have to be empty.

*

No exceptions are thrown when a file or directory cannot be deleted.

*

*

*@paramfile

* file or directory to delete, can be {@codenull}

*@return{@codetrue} if the file or directory was deleted, otherwise

* {@codefalse}

**/

public static booleandeleteQuietly(File file) {if (file == null) {return false;

}try{if(file.isDirectory()) {

cleanDirectory(file);

}

}catch(Exception ignored) {

}try{returnfile.delete();

}catch(Exception ignored) {return false;

}

}/*** Makes a directory, including any necessary but nonexistent parent

* directories. If a file already exists with specified name but it is

* not a directory then an IOException is thrown.

* If the directory cannot be created (or does not already exist)

* then an IOException is thrown.

*

*@paramdirectory

* directory to create, must not be {@codenull}

*@throwsNullPointerException

* if the directory is {@codenull}

*@throwsIOException

* if the directory cannot be created or the file already exists

* but is not a directory*/

public static void forceMkdir(File directory) throwsIOException {if(directory.exists()) {if (!directory.isDirectory()) {

String message= "File " + directory + " exists and is "

+ "not a directory. Unable to create directory.";throw newIOException(message);

}

}else{if (!directory.mkdirs()) {//Double-check that some other thread or process hasn't made//the directory in the background

if (!directory.isDirectory()) {

String message= "Unable to create directory " +directory;throw newIOException(message);

}

}

}

}/*** Returns the size of the specified file or directory. If the provided

* {@linkFile} is a regular file, then the file's length is returned.

* If the argument is a directory, then the size of the directory is

* calculated recursively. If a directory or subdirectory is security

* restricted, its size will not be included.

*

*@paramfile

* the regular file or directory to return the size

* of (must not be {@codenull}).

*

*@returnthe length of the file, or recursive size of the directory,

* provided (in bytes).

*

*@throwsNullPointerException

* if the file is {@codenull}

*@throwsIllegalArgumentException

* if the file does not exist.

**/

public static longsizeOf(File file) {if (!file.exists()) {

String message= file + " does not exist";throw newIllegalArgumentException(message);

}if(file.isDirectory()) {returnsizeOfDirectory(file);

}else{returnfile.length();

}

}/*** Counts the size of a directory recursively (sum of the length of all

* files).

*

*@paramdirectory

* directory to inspect, must not be {@codenull}

*@returnsize of directory in bytes, 0 if directory is security

* restricted, a negative number when the real total

* is greater than {@linkLong#MAX_VALUE}.

*@throwsNullPointerException

* if the directory is {@codenull}*/

public static longsizeOfDirectory(File directory) {

checkDirectory(directory);final File[] files =directory.listFiles();if (files == null) { //null if security restricted

return 0L;

}long size = 0;for (finalFile file : files) {

size+=sizeOf(file);if (size < 0) {break;

}

}returnsize;

}/*** Checks that the given {@codeFile} exists and is a directory.

*

*@paramdirectory

* The {@codeFile} to check.

*@throwsIllegalArgumentException

* if the given {@codeFile} does not exist or is not a

* directory.*/

private static voidcheckDirectory(File directory) {if (!directory.exists()) {throw new IllegalArgumentException(directory + " does not exist");

}if (!directory.isDirectory()) {throw newIllegalArgumentException(directory+ " is not a directory");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值