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
35
36
37
38
39 public class FileUtils {
40
41
42
43
44
45
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
74
75
76
77
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
102
103
104
105 public static String getFilenameWithoutPath(final String filename) {
106 final File f = new File(filename);
107 return f.getName();
108 }
109
110
111
112
113
114 public static String getDirSeparator() {
115 return System.getProperty("file.separator");
116 }
117
118
119
120
121
122 public static String getDefaultDirSeparator() {
123
124 return java.io.File.separator;
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
153
154
155
156
157
158
159 public static String getAbsoluteName(final String path, final String filename) {
160 return path + getDirSeparator() + filename;
161 }
162
163
164
165
166
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
182
183
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 }