package com.walker.infrastructure.utils;
|
|
import java.io.File;
|
import java.io.IOException;
|
|
/**
|
* Utility methods for working with the file system.
|
*
|
* @author Rob Harrop
|
* @author Juergen Hoeller
|
* @since 2.5.3
|
*/
|
public abstract class FileSystemUtils {
|
|
public static final String FILE_SCHEMA_WIN = "file:/";
|
public static final String FILE_SCHEMA_STD = "file:///";
|
|
/**
|
* 去掉文件路径中的协议信息,如:
|
* <pre>
|
* file:///d:/test/my.log 去掉file:///
|
* 在windows系统中也可能是file:/
|
* </pre>
|
* @param path 给定文件全路径信息
|
* @return 返回修剪后的路径
|
*/
|
public static String trimFileSchema(String path){
|
if(path.startsWith(FILE_SCHEMA_WIN)){
|
path = path.substring(6);
|
} else if(path.startsWith(FILE_SCHEMA_STD)){
|
path = path.substring(8);
|
}
|
return path;
|
}
|
|
/**
|
* Delete the supplied {@link File} - for directories,
|
* recursively delete any nested directories or files as well.
|
* @param root the root <code>File</code> to delete
|
* @return <code>true</code> if the <code>File</code> was deleted,
|
* otherwise <code>false</code>
|
*/
|
public static boolean deleteRecursively(File root) {
|
if (root != null && root.exists()) {
|
if (root.isDirectory()) {
|
File[] children = root.listFiles();
|
if (children != null) {
|
for (File child : children) {
|
deleteRecursively(child);
|
}
|
}
|
}
|
return root.delete();
|
}
|
return false;
|
}
|
|
/**
|
* Recursively copy the contents of the <code>src</code> file/directory
|
* to the <code>dest</code> file/directory.
|
* @param src the source directory
|
* @param dest the destination directory
|
* @throws IOException in the case of I/O errors
|
*/
|
public static void copyRecursively(File src, File dest) throws IOException {
|
Assert.isTrue(src != null && (src.isDirectory() || src.isFile()), "Source File must denote a directory or file");
|
Assert.notNull(dest, "Destination File must not be null");
|
doCopyRecursively(src, dest);
|
}
|
|
/**
|
* Actually copy the contents of the <code>src</code> file/directory
|
* to the <code>dest</code> file/directory.
|
* @param src the source directory
|
* @param dest the destination directory
|
* @throws IOException in the case of I/O errors
|
*/
|
private static void doCopyRecursively(File src, File dest) throws IOException {
|
if (src.isDirectory()) {
|
dest.mkdir();
|
File[] entries = src.listFiles();
|
if (entries == null) {
|
throw new IOException("Could not list files in directory: " + src);
|
}
|
for (File entry : entries) {
|
doCopyRecursively(entry, new File(dest, entry.getName()));
|
}
|
}
|
else if (src.isFile()) {
|
try {
|
dest.createNewFile();
|
}
|
catch (IOException ex) {
|
IOException ioex = new IOException("Failed to create file: " + dest);
|
ioex.initCause(ex);
|
throw ioex;
|
}
|
FileCopyUtils.copy(src, dest);
|
}
|
else {
|
// Special File handle: neither a file not a directory.
|
// Simply skip it when contained in nested directory...
|
}
|
}
|
|
public static void main(String[] args) throws Exception{
|
File file = new File("d:/report.log");
|
if(file.exists()){
|
System.out.println("ok.");
|
} else
|
System.out.println("? not found");
|
System.out.println(file.getAbsolutePath());
|
System.out.println(file.getCanonicalPath());
|
System.out.println(file.toURI());
|
}
|
}
|