shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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());
    }
}