View Javadoc

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          //empty
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); // Get File objects from Strings
86          File toFile = new File(toName);
87  
88          // First make sure the source file exists, is a file, and is readable.
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          // If the destination is a directory, use the source file name
100         // as the destination file name
101         if (toFile.isDirectory()) {
102             toFile = new File(toFile, fromFile.getName());
103         }
104 
105         // If the destination exists, make sure it is a writeable file.
106         // If the destination doesn't
107         // exist, make sure the directory exists and is writeable.
108         if (toFile.exists()) {
109             if (!toFile.canWrite()) {
110                 abort("FileCopy: destination file is unwriteable: " + toName);
111             }
112         } else {
113             // if file doesn't exist, check if directory exists and is
114             // writeable.
115             // If getParent() returns null, then the directory is the current
116             // dir.
117             // so look up the user.dir system property to find out what that is.
118             String parent = toFile.getParent();
119             // Get the destination directory
120             if (parent == null) {
121                 parent = System.getProperty("user.dir"); // or CWD
122             }
123             File dir = new File(parent); // Convert it to a file.
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         // If we've gotten this far, then everything is okay.
139         // So we copy the file, a buffer of bytes at a time.
140         FileInputStream from = null; // Stream to read from source
141         FileOutputStream to = null; // Stream to write to destination
142         try {
143             from = new FileInputStream(fromFile); // Create input stream
144             to = new FileOutputStream(toFile); // Create output stream
145             final int bufferSize = 4096;
146             byte[] buffer = new byte[bufferSize]; // to hold file contents
147             int bytesRead; // How many bytes in buffer
148             // Read a chunk of bytes into the buffer, then write them out,
149             // looping until we reach the end of the file (when read() returns
150             // -1).
151             // Note the combination of assignment and comparison in this while
152             // loop.  This is a common I/O programming idiom.
153             while ((bytesRead = from.read(buffer)) != -1) {
154                 // Read bytes until EOF
155                 to.write(buffer, 0, bytesRead); //   write bytes
156             }
157         } finally { // Always close the streams, even if exceptions were thrown
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 }