1 package de.matthias_burbach.mosaique.core.util;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 /***
9 * @author Matthias Burbach
10 */
11 public final class FileUtils {
12 /***
13 * Private default constructor to enforce static usage of this utility
14 * class.
15 */
16 private FileUtils() {
17
18 }
19
20 /***
21 * @param pathAndName The path and name of the file to get the path for.
22 * @return The path of the file.
23 */
24 public static String getPathWithoutName(final String pathAndName) {
25 if (pathAndName == null) {
26 return null;
27 }
28 String name = (new File(pathAndName)).getName();
29 return pathAndName.substring(0, pathAndName.length() - name.length());
30 }
31
32 /***
33 * @param pathAndName The path and name of the file to get the name for.
34 * @return The name of the file.
35 */
36 public static String getNameWithoutPath(final String pathAndName) {
37 if (pathAndName == null) {
38 return null;
39 }
40 String name = (new File(pathAndName)).getName();
41 return name;
42 }
43
44 /***
45 * @param filePathAndName The file to check for validity.
46 * @return <code>true</code> iff the file exists and is readable
47 */
48 public static boolean isValidFile(final String filePathAndName) {
49 if (filePathAndName == null) {
50 return false;
51 }
52
53 File aFile = new File(filePathAndName);
54 if (!aFile.exists() || !aFile.canRead() || !aFile.isFile()) {
55 return false;
56 }
57
58 return true;
59 }
60
61 /***
62 * @param file The file to delete.
63 */
64 public static void delete(final String file) {
65 File aFile = new File(file);
66 aFile.delete();
67 }
68
69 /***
70 * Performs a file copy.
71 * Before copying the file, however, it performs a lot of tests to make
72 * sure everything is as it should be.
73 *
74 * @param fromName The path and name of the file to copy from.
75 * @param toName The path and name of the file to copy to.
76 *
77 * @throws IOException if copying fails for any reason
78 *
79 * Copyright (c) 1997 by David Flanagan
80 */
81 public static void copy(
82 final String fromName,
83 final String toName)
84 throws IOException {
85 File fromFile = new File(fromName);
86 File toFile = new File(toName);
87
88
89 if (!fromFile.exists()) {
90 abort("FileCopy: no such source file: " + fromName);
91 }
92 if (!fromFile.isFile()) {
93 abort("FileCopy: can't copy directory: " + fromName);
94 }
95 if (!fromFile.canRead()) {
96 abort("FileCopy: source file is unreadable: " + fromName);
97 }
98
99
100
101 if (toFile.isDirectory()) {
102 toFile = new File(toFile, fromFile.getName());
103 }
104
105
106
107
108 if (toFile.exists()) {
109 if (!toFile.canWrite()) {
110 abort("FileCopy: destination file is unwriteable: " + toName);
111 }
112 } else {
113
114
115
116
117
118 String parent = toFile.getParent();
119
120 if (parent == null) {
121 parent = System.getProperty("user.dir");
122 }
123 File dir = new File(parent);
124 if (!dir.exists()) {
125 abort(
126 "FileCopy: destination directory doesn't exist: " + parent);
127 }
128 if (dir.isFile()) {
129 abort("FileCopy: destination is not a directory: " + parent);
130 }
131 if (!dir.canWrite()) {
132 abort(
133 "FileCopy: destination directory is unwriteable: "
134 + parent);
135 }
136 }
137
138
139
140 FileInputStream from = null;
141 FileOutputStream to = null;
142 try {
143 from = new FileInputStream(fromFile);
144 to = new FileOutputStream(toFile);
145 final int bufferSize = 4096;
146 byte[] buffer = new byte[bufferSize];
147 int bytesRead;
148
149
150
151
152
153 while ((bytesRead = from.read(buffer)) != -1) {
154
155 to.write(buffer, 0, bytesRead);
156 }
157 } finally {
158 if (from != null) {
159 try {
160 from.close();
161 } catch (IOException e) {
162 e.printStackTrace();
163 }
164 }
165 if (to != null) {
166 try {
167 to.close();
168 } catch (IOException e) {
169 e.printStackTrace();
170 }
171 }
172 }
173 }
174
175 /***
176 * Is a convenience method to throw an exception.
177 * @param msg The message to deliver on abort.
178 * @throws IOException always
179 */
180 private static void abort(final String msg) throws IOException {
181 throw new IOException(msg);
182 }
183 }