gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.lib / org.gvsig.scripting.lib.impl / src / main / java / org / gvsig / scripting / impl / FileUtils.java @ 1084
History | View | Annotate | Download (3.42 KB)
1 | 989 | jjdelcerro | package org.gvsig.scripting.impl; |
---|---|---|---|
2 | |||
3 | import java.io.File; |
||
4 | import java.io.IOException; |
||
5 | import java.util.List; |
||
6 | import org.apache.commons.io.FilenameUtils; |
||
7 | |||
8 | public class FileUtils { |
||
9 | |||
10 | public static String getRealPath(File file) { |
||
11 | try {
|
||
12 | return file.getCanonicalPath();
|
||
13 | } catch (IOException ex) { |
||
14 | return FilenameUtils.normalizeNoEndSeparator(file.getAbsolutePath(), true); |
||
15 | } |
||
16 | } |
||
17 | |||
18 | public static File getRealFile(File file) { |
||
19 | try {
|
||
20 | return file.getCanonicalFile();
|
||
21 | } catch (IOException ex) { |
||
22 | return new File(FilenameUtils.normalizeNoEndSeparator(file.getAbsolutePath(), true)); |
||
23 | } |
||
24 | } |
||
25 | |||
26 | public static File getAbsoluteFile(File file) { |
||
27 | return new File(FilenameUtils.normalizeNoEndSeparator(file.getAbsolutePath(), true)); |
||
28 | } |
||
29 | |||
30 | public static File getAbsoluteFile(String pathname) { |
||
31 | File file = new File(pathname); |
||
32 | return new File(FilenameUtils.normalizeNoEndSeparator(file.getAbsolutePath(), true)); |
||
33 | } |
||
34 | |||
35 | public static String getBaseName(File f) { |
||
36 | return FilenameUtils.getBaseName(f.getName());
|
||
37 | } |
||
38 | |||
39 | public static boolean isRootFile(File f) { |
||
40 | String pathname = getRealPath(f);
|
||
41 | String prefix = FilenameUtils.getPrefix(pathname);
|
||
42 | return pathname.equals(prefix);
|
||
43 | } |
||
44 | |||
45 | public static boolean isSameFile(File f1, File f2) { |
||
46 | String f1Path = getRealPath(f1);
|
||
47 | String f2Path = getRealPath(f2);
|
||
48 | return f1Path.equals(f2Path);
|
||
49 | } |
||
50 | |||
51 | public static boolean isSubfolder(File folder, File file) { |
||
52 | String folderPath = getRealPath(folder)+"/"; |
||
53 | String filePath = getRealPath(file);
|
||
54 | return filePath.startsWith(folderPath);
|
||
55 | } |
||
56 | |||
57 | public static File removeExtension(File f) { |
||
58 | String pathname = FilenameUtils.removeExtension(f.getPath());
|
||
59 | return getAbsoluteFile(pathname);
|
||
60 | } |
||
61 | |||
62 | static void forceMkdir(File f) throws IOException { |
||
63 | org.apache.commons.io.FileUtils.forceMkdir(f); |
||
64 | } |
||
65 | |||
66 | static File getFile(File folder, String... names) { |
||
67 | return org.apache.commons.io.FileUtils.getFile(folder, names);
|
||
68 | } |
||
69 | |||
70 | static List<String> readLines(File file) throws IOException { |
||
71 | return org.apache.commons.io.FileUtils.readLines(file);
|
||
72 | } |
||
73 | |||
74 | static void writeStringToFile(File file, String contents) throws IOException { |
||
75 | org.apache.commons.io.FileUtils.writeStringToFile(file, contents); |
||
76 | } |
||
77 | |||
78 | static void forceDelete(File f) throws IOException { |
||
79 | org.apache.commons.io.FileUtils.forceDelete(f); |
||
80 | } |
||
81 | |||
82 | static void moveFileToDirectory(File srcFile, File destDir, boolean createDestDir) throws IOException { |
||
83 | org.apache.commons.io.FileUtils.moveFileToDirectory(srcFile, destDir, createDestDir); |
||
84 | } |
||
85 | |||
86 | static void moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) throws IOException { |
||
87 | org.apache.commons.io.FileUtils.moveDirectoryToDirectory(src, destDir, createDestDir); |
||
88 | } |
||
89 | |||
90 | static void moveFile(File srcFile, File destFile) throws IOException { |
||
91 | org.apache.commons.io.FileUtils.moveFile(srcFile, destFile); |
||
92 | } |
||
93 | |||
94 | static void moveDirectory(File srcDir, File destDir) throws IOException { |
||
95 | org.apache.commons.io.FileUtils.moveDirectory(srcDir, destDir); |
||
96 | } |
||
97 | |||
98 | 1084 | jjdelcerro | static void copyDirectory(File srcDir, File destDir) throws IOException { |
99 | org.apache.commons.io.FileUtils.copyDirectory(srcDir, destDir); |
||
100 | } |
||
101 | |||
102 | 989 | jjdelcerro | } |