IO 공구 꾸러미 commons - io
7986 단어 Java
commons-io
commons-io
2.4
FileUtils
/**
* writing to a file
* reading from a file
* make a directory including parent directories
* copying files and directories
* deleting files and directories
* converting to and from a URL
* listing files and directories by filter and extension
* comparing file content
* file last changed date
* calculating a checksum
*
* @version $Id: FileUtils.java 1349509 2012-06-12 20:39:23Z ggregory $
*/
public class FileUtils {
/**
* Instances should NOT be constructed in standard programming.
*/
public FileUtils() {
super();
}
/**
* directory/name[0]/name[1]/...
*/
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;
}
/**
* name[0]/name[1]/...
*/
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;
}
/**
*
*/
public static String getTempDirectoryPath() {
return System.getProperty("java.io.tmpdir");
}
public static File getTempDirectory() {
return new File(getTempDirectoryPath());
}
/**
* home
*/
public static String getUserDirectoryPath() {
return System.getProperty("user.home");
}
public static File getUserDirectory() {
return new File(getUserDirectoryPath());
}
/**
* ,
*/
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);
}
public static FileOutputStream openOutputStream(File file) throws IOException {
return openOutputStream(file, false);
}
/**
* ,
*/
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 String byteCountToDisplaySize(BigInteger size) {
String displaySize;
if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_EB_BI)) + " EB";
} else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_PB_BI)) + " PB";
} else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_TB_BI)) + " TB";
} else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB";
} else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB";
} else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB";
} else {
displaySize = String.valueOf(size) + " bytes";
}
return displaySize;
}
public static String byteCountToDisplaySize(long size) {
return byteCountToDisplaySize(BigInteger.valueOf(size));
}
/**
*
*/
public static void touch(File file) throws IOException {
if (!file.exists()) {
OutputStream out = openOutputStream(file);
IOUtils.closeQuietly(out);
}
boolean success = file.setLastModified(System.currentTimeMillis());
if (!success) {
throw new IOException("Unable to set the last modification time for " + file);
}
}
public static File[] convertFileCollectionToFileArray(Collection files) {
return files.toArray(new File[files.size()]);
}
private static void innerListFiles(Collection files, File directory,
IOFileFilter filter, boolean includeSubDirectories) {
File[] found = directory.listFiles((FileFilter) filter);
if (found != null) {
for (File file : found) {
if (file.isDirectory()) {
if (includeSubDirectories) {
files.add(file);
}
innerListFiles(files, file, filter, includeSubDirectories);
} else {
files.add(file);
}
}
}
}
/**
* , filter
*/
public static Collection listFiles(
File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
validateListFilesParameters(directory, fileFilter);
IOFileFilter effFileFilter = setUpEffectiveFileFilter(fileFilter);
IOFileFilter effDirFilter = setUpEffectiveDirFilter(dirFilter);
//Find files
Collection files = new java.util.LinkedList();
innerListFiles(files, directory,
FileFilterUtils.or(effFileFilter, effDirFilter), false);
return files;
}
public static Collection listFilesAndDirs(
File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
validateListFilesParameters(directory, fileFilter);
IOFileFilter effFileFilter = setUpEffectiveFileFilter(fileFilter);
IOFileFilter effDirFilter = setUpEffectiveDirFilter(dirFilter);
//Find files
Collection files = new java.util.LinkedList();
if (directory.isDirectory()) {
files.add(directory);
}
innerListFiles(files, directory,
FileFilterUtils.or(effFileFilter, effDirFilter), true);
return files;
}
//... / 、 、 、 、
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.