1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.statcvs.util;
24
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.FileReader;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.io.InputStream;
32
33 /**
34 * Some helpful file functions
35 * TODO: Remove redundancy, write tests
36 * @author Lukasz Pekacki
37 * @version $Id: FileUtils.java,v 1.20 2008/04/02 11:52:02 benoitx Exp $
38 */
39 public class FileUtils {
40 /**
41 * Copyies a file to a specified desitination
42 * @param inputName File
43 * @param destination Filename
44 * @throws FileNotFoundException if no input file exists
45 * @throws IOException if cannot read or write
46 */
47 public static void copyFile(final String inputName, final String destination) throws FileNotFoundException, IOException {
48 final File input = new File(inputName);
49 final File outputFile = new File(destination);
50 FileReader in = null;
51 FileWriter out = null;
52 try {
53 in = new FileReader(input);
54 out = new FileWriter(outputFile);
55 int c;
56 while ((c = in.read()) != -1) {
57 out.write(c);
58 }
59 } finally {
60 try {
61 if (in != null) {
62 in.close();
63 }
64 } finally {
65 if (out != null) {
66 out.close();
67 }
68 }
69 }
70 }
71
72 /**
73 * Copy a InputStream into a File
74 * @param in source
75 * @param out destination
76 * @throws FileNotFoundException if not found
77 * @throws IOException if read/write error
78 */
79 public static void copyFile(final InputStream in, final File out) throws FileNotFoundException, IOException {
80 final InputStream fis = in;
81 FileOutputStream fos = null;
82 try {
83 fos = new FileOutputStream(out);
84 final byte[] buf = new byte[1024];
85 int i = 0;
86 while ((i = fis.read(buf)) > 0) {
87 fos.write(buf, 0, i);
88 }
89 } finally {
90 try {
91 fis.close();
92 } finally {
93 if (fos != null) {
94 fos.close();
95 }
96 }
97 }
98 }
99
100 /**
101 * Takes a filename with path and returns just the filename.
102 * @param filename a filename with path
103 * @return just the filename part
104 */
105 public static String getFilenameWithoutPath(final String filename) {
106 final File f = new File(filename);
107 return f.getName();
108 }
109
110 /**
111 * Returns the os dependend path seperator
112 * @return String os dependend path seperator
113 */
114 public static String getDirSeparator() {
115 return System.getProperty("file.separator");
116 }
117
118 /**
119 * Returns the java path seperator
120 * @return String java path seperator
121 */
122 public static String getDefaultDirSeparator() {
123
124 return java.io.File.separator;
125 }
126
127 /**
128 * Deletes the ending directory separator of a
129 * given <code>path</code> if there is one and returns
130 * the result.
131 * Otherwise the path is unhandled returned.
132 *
133 * <p>The separator is the one used bye the
134 * underlying operating system and it is the one returned
135 * bye the <code>getDirSeparator()</code> method.
136 *
137 * @param path The <code>path</code> to delete the directory
138 * separator from.
139 * @return The <code>path</code> without the ending
140 * directory separator.
141 * @see net.sf.statcvs.util.FileUtils#getDirSeparator
142 */
143 public static String getPathWithoutEndingSlash(final String path) {
144 if (path.endsWith(getDefaultDirSeparator())) {
145 final int pos = path.lastIndexOf(getDefaultDirSeparator());
146 return path.substring(0, pos);
147 }
148 return path;
149 }
150
151 /**
152 * Concatenates <code>path</code> and filename to an
153 * absolute filename by inserting the system file separator.
154 *
155 * @param path The path to use.
156 * @param filename The filename for concatenation.
157 * @return The concatenated absolute filename.
158 */
159 public static String getAbsoluteName(final String path, final String filename) {
160 return path + getDirSeparator() + filename;
161 }
162
163 /**
164 * Returns the last compontent of a directory path.
165 * @param path a directory, ending in "/", for example "src/net/sf/statcvs/"
166 * @return the last component of the path, for example "statcvs"
167 */
168 public static String getDirectoryName(final String path) {
169 if ("".equals(path)) {
170 throw new IllegalArgumentException("can't get directory name for root");
171 }
172 final String pathWithoutLastSlash = path.substring(0, path.length() - 1);
173 final int lastSlash = pathWithoutLastSlash.lastIndexOf('/');
174 if (lastSlash == -1) {
175 return pathWithoutLastSlash;
176 }
177 return pathWithoutLastSlash.substring(lastSlash + 1);
178 }
179
180 /**
181 * Returns all but the last compontent of a directory path
182 * @param path a directory, ending in "/", for example "src/net/sf/statcvs/"
183 * @return all but the last component of the path, for example "src/net/sf/"
184 */
185 public static String getParentDirectoryPath(final String path) {
186 if ("".equals(path)) {
187 throw new IllegalArgumentException("can't get directory name for root");
188 }
189 final String pathWithoutLastSlash = path.substring(0, path.length() - 1);
190 final int lastSlash = pathWithoutLastSlash.lastIndexOf('/');
191 if (lastSlash == -1) {
192 return "";
193 }
194 return pathWithoutLastSlash.substring(0, lastSlash + 1);
195 }
196 }