Java File類總結和FileUtils類
文件存在和類型判斷
創建出File類的對象並不代表該路徑下有此文件或目錄。
用public boolean exists()可以判斷文件是否存在。
File類的對象可以是目錄或者文件。
如果是目錄,public boolean isDirectory()返回true;
如果是文件(非目錄則是文件),public boolean isFile()返回true;
但是注意需要先判斷文件是否存在,如果文件不存在,上面兩個方法都返回false,即不存在的File類對象既不是文件也不是目錄。
創建文件
public boolean createNewFile()會創建一個新的空文件,只有該文件不存在的時候會創建,如果文件已經存在的話則返回false。
創建文件夾
public boolean mkdir()
創建目錄,成功返回true。只能創建一個文件夾,要求所有的父目錄都存在,否則創建失敗。
public boolean mkdirs()
創建目錄,成功返回true,會創建所有不存在的父目錄。(注意即便最后創建失敗,但是也可能創建了一些中間目錄)。
上面兩個方法如果要創建的目錄已經存在,不再重新創建,都返回false,只有新建目錄返回true。
目錄操作
列出目錄中的文件有以下方法可選:
String[] list()
String[] list(FilenameFilter filter)
返回文件名數組。
File[] listFiles()
File[] listFiles(FileFilter filter)
File[] listFiles(FilenameFilter filter)
返回File數組。
參數是文件或者文件名過濾器。
注意返回為空和返回為null的意義是不同的。
若不包含(符合條件的)文件,返回為空。
但是如果返回為null,則表明調用方法的File對象可能不是一個目錄,或者發生了IO錯誤。
刪除文件
boolean delete()方法會刪除文件,如果File對象是文件則直接刪除,對於目錄來說,如果是空目錄則直接刪除,非空目錄則無法刪除,返回false。
如果要刪除的文件不能被刪除則會拋出IOException。
注意:不論是創建文件、創建目錄還是刪除文件,只有在動作真正發生的時候會返回true。
FileUtils類
在項目中寫一些工具類包裝通用操作是很有必要的,看了一下apache的FileUtils類,copy了一些方法出來:

/* * 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. */ package com.mengdd.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* * FileUtils copied from org.apache.commons.io.FileUtils */ public class FileUtils { /** * Construct a file from the set of name elements. * * @param directory * the parent directory * @param names * the name elements * @return the file */ public static File getFile(File directory, String... names) { if (directory == null) { throw new NullPointerException( "directorydirectory must not be null"); } if (names == null) { throw new NullPointerException("names must not be null"); } File file = directory; for (String name : names) { file = new File(file, name); } return file; } /** * Construct a file from the set of name elements. * * @param names * the name elements * @return the file */ public static File 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 = new File(name); } else { file = new File(file, name); } } return file; } /** * Opens a {@link FileInputStream} for the specified file, providing better * error messages than simply calling <code>new FileInputStream(file)</code> * . * <p> * At the end of the method either the stream will be successfully opened, * or an exception will have been thrown. * <p> * 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. * * @param file * the file to open for input, must not be {@code null} * @return a new {@link FileInputStream} for the specified file * @throws FileNotFoundException * if the file does not exist * @throws IOException * if the file object is a directory * @throws IOException * if the file cannot be read */ public static FileInputStream openInputStream(File file) throws IOException { 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 new FileInputStream(file); } /** * Opens a {@link FileOutputStream} for the specified file, checking and * creating the parent directory if it does not exist. * <p> * At the end of the method either the stream will be successfully opened, * or an exception will have been thrown. * <p> * 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. * * @param file * the file to open for output, must not be {@code null} * @param append * if {@code true}, then bytes will be added to the * end of the file rather than overwriting * @return a new {@link FileOutputStream} for the specified file * @throws IOException * if the file object is a directory * @throws IOException * if the file cannot be written to * @throws IOException * if a parent directory needs creating but that fails */ public static FileOutputStream openOutputStream(File file, boolean append) throws IOException { 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 new FileOutputStream(file, append); } public static FileOutputStream openOutputStream(File file) throws IOException { return openOutputStream(file, false); } /** * Cleans a directory without deleting it. * * @param directory * directory to clean * @throws IOException * in case cleaning is unsuccessful */ public static void cleanDirectory(File directory) throws IOException { if (!directory.exists()) { String message = directory + " does not exist"; throw new IllegalArgumentException(message); } if (!directory.isDirectory()) { String message = directory + " is not a directory"; throw new IllegalArgumentException(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) { throw exception; } } // ----------------------------------------------------------------------- /** * Deletes a directory recursively. * * @param directory * directory to delete * @throws IOException * in case deletion is unsuccessful */ public static void deleteDirectory(File directory) throws IOException { if (!directory.exists()) { return; } cleanDirectory(directory); if (!directory.delete()) { String message = "Unable to delete directory " + directory + "."; throw new IOException(message); } } /** * Deletes a file. If file is a directory, delete it and all * sub-directories. * <p> * The difference between File.delete() and this method are: * <ul> * <li>A directory to be deleted does not have to be empty.</li> * <li>You get exceptions when a file or directory cannot be deleted. * (java.io.File methods returns a boolean)</li> * </ul> * * @param file * file or directory to delete, must not be {@code null} * @throws NullPointerException * if the directory is {@code null} * @throws FileNotFoundException * if the file was not found * @throws IOException * in case deletion is unsuccessful */ public static void forceDelete(File file) throws IOException { 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 new IOException(message); } } } /** * Deletes a file, never throwing an exception. If file is a directory, * delete it and all sub-directories. * <p> * The difference between File.delete() and this method are: * <ul> * <li>A directory to be deleted does not have to be empty.</li> * <li>No exceptions are thrown when a file or directory cannot be deleted.</li> * </ul> * * @param file * file or directory to delete, can be {@code null} * @return {@code true} if the file or directory was deleted, otherwise * {@code false} * */ public static 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; } } /** * 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. * * @param directory * directory to create, must not be {@code null} * @throws NullPointerException * if the directory is {@code null} * @throws IOException * if the directory cannot be created or the file already exists * but is not a directory */ public static void forceMkdir(File directory) throws IOException { if (directory.exists()) { if (!directory.isDirectory()) { String message = "File " + directory + " exists and is " + "not a directory. Unable to create directory."; throw new IOException(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 new IOException(message); } } } } /** * Returns the size of the specified file or directory. If the provided * {@link File} 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. * * @param file * the regular file or directory to return the size * of (must not be {@code null}). * * @return the length of the file, or recursive size of the directory, * provided (in bytes). * * @throws NullPointerException * if the file is {@code null} * @throws IllegalArgumentException * if the file does not exist. * */ public static long sizeOf(File file) { if (!file.exists()) { String message = file + " does not exist"; throw new IllegalArgumentException(message); } if (file.isDirectory()) { return sizeOfDirectory(file); } else { return file.length(); } } /** * Counts the size of a directory recursively (sum of the length of all * files). * * @param directory * directory to inspect, must not be {@code null} * @return size of directory in bytes, 0 if directory is security * restricted, a negative number when the real total * is greater than {@link Long#MAX_VALUE}. * @throws NullPointerException * if the directory is {@code null} */ public static long sizeOfDirectory(File directory) { checkDirectory(directory); final File[] files = directory.listFiles(); if (files == null) { // null if security restricted return 0L; } long size = 0; for (final File file : files) { size += sizeOf(file); if (size < 0) { break; } } return size; } /** * Checks that the given {@code File} exists and is a directory. * * @param directory * The {@code File} to check. * @throws IllegalArgumentException * if the given {@code File} does not exist or is not a * directory. */ private static void checkDirectory(File directory) { if (!directory.exists()) { throw new IllegalArgumentException(directory + " does not exist"); } if (!directory.isDirectory()) { throw new IllegalArgumentException(directory + " is not a directory"); } } }
參考資料
File類官方文檔:
http://docs.oracle.com/javase/7/docs/api/java/io/File.html
org.apache.commons.io.FileUtils源碼:
本博客舊博文:
Java IO 用遞歸實現目錄刪除和樹形目錄展示 Java實現