2
2
3
3
import org .apache .commons .compress .utils .IOUtils ;
4
4
5
- import java .io .*;
5
+ import java .io .BufferedReader ;
6
+ import java .io .File ;
7
+ import java .io .FileInputStream ;
8
+ import java .io .FileOutputStream ;
9
+ import java .io .IOException ;
10
+ import java .io .InputStreamReader ;
6
11
import java .nio .file .Files ;
7
12
import java .nio .file .Paths ;
8
- import java .util .*;
9
- import java .util .regex .Pattern ;
13
+ import java .util .ArrayList ;
14
+ import java .util .Arrays ;
15
+ import java .util .List ;
16
+ import java .util .Random ;
10
17
11
18
public class FileUtils {
12
19
13
20
private static final List <String > SOURCE_CONTROL_FOLDERS = Arrays .asList ("CVS" , "RCS" , ".git" , ".svn" , ".hg" , ".bzr" );
14
- private static final Pattern BACKSLASH = Pattern .compile ("\\ \\ " );
15
21
16
22
/**
17
23
* Checks, whether the child directory is a subdirectory of the base directory.
@@ -109,75 +115,6 @@ public static File createTempFolder(File parent, String prefix, String suffix) t
109
115
return Files .createDirectories (Paths .get (parent .getAbsolutePath (), prefix + suffix )).toFile ();
110
116
}
111
117
112
- //
113
- // Compute relative path to "target" from a directory "origin".
114
- //
115
- // If "origin" is not absolute, it is relative from the current directory.
116
- // If "target" is not absolute, it is relative from "origin".
117
- //
118
- // by Shigeru KANEMOTO at SWITCHSCIENCE.
119
- //
120
- public static String relativePath (String origin , String target ) {
121
- try {
122
- origin = (new File (origin )).getCanonicalPath ();
123
- File targetFile = new File (target );
124
- if (targetFile .isAbsolute ())
125
- target = targetFile .getCanonicalPath ();
126
- else
127
- target = (new File (origin , target )).getCanonicalPath ();
128
- } catch (IOException e ) {
129
- return null ;
130
- }
131
-
132
- if (origin .equals (target )) {
133
- // origin and target is identical.
134
- return "." ;
135
- }
136
-
137
- if (origin .equals (File .separator )) {
138
- // origin is root.
139
- return "." + target ;
140
- }
141
-
142
- String prefix = "" ;
143
- String root = File .separator ;
144
-
145
- if (System .getProperty ("os.name" ).indexOf ("Windows" ) != -1 ) {
146
- if (origin .startsWith ("\\ \\ " ) || target .startsWith ("\\ \\ " )) {
147
- // Windows UNC path not supported.
148
- return null ;
149
- }
150
-
151
- char originLetter = origin .charAt (0 );
152
- char targetLetter = target .charAt (0 );
153
- if (Character .isLetter (originLetter ) && Character .isLetter (targetLetter )) {
154
- // Windows only
155
- if (originLetter != targetLetter ) {
156
- // Drive letters differ
157
- return null ;
158
- }
159
- }
160
-
161
- prefix = "" + originLetter + ':' ;
162
- root = prefix + File .separator ;
163
- }
164
-
165
- String relative = "" ;
166
- while (!target .startsWith (origin + File .separator )) {
167
- origin = (new File (origin )).getParent ();
168
- if (origin .equals (root ))
169
- origin = prefix ;
170
- relative += ".." ;
171
- relative += File .separator ;
172
- }
173
-
174
- return relative + target .substring (origin .length () + 1 );
175
- }
176
-
177
- public static String getLinuxPathFrom (File file ) {
178
- return BACKSLASH .matcher (file .getAbsolutePath ()).replaceAll ("/" );
179
- }
180
-
181
118
public static boolean isSCCSOrHiddenFile (File file ) {
182
119
return isSCCSFolder (file ) || isHiddenFile (file );
183
120
}
@@ -209,25 +146,6 @@ public static String readFileToString(File file, String encoding) throws IOExcep
209
146
}
210
147
}
211
148
212
- public static List <String > readFileToListOfStrings (File file ) throws IOException {
213
- List <String > strings = new LinkedList <>();
214
- BufferedReader reader = null ;
215
- try {
216
- reader = new BufferedReader (new FileReader (file ));
217
- String line ;
218
- while ((line = reader .readLine ()) != null ) {
219
- line = line .replaceAll ("\r " , "" ).replaceAll ("\n " , "" ).replaceAll (" " , "" );
220
- strings .add (line );
221
- }
222
- return strings ;
223
- } finally {
224
- if (reader != null ) {
225
- reader .close ();
226
- }
227
- }
228
- }
229
-
230
-
231
149
/**
232
150
* Returns true if the given file has any of the given extensions.
233
151
*
@@ -236,10 +154,6 @@ public static List<String> readFileToListOfStrings(File file) throws IOException
236
154
* dot). Should all be lowercase, case insensitive matching
237
155
* is used.
238
156
*/
239
- public static boolean hasExtension (File file , String ... extensions ) {
240
- return hasExtension (file , Arrays .asList (extensions ));
241
- }
242
-
243
157
public static boolean hasExtension (File file , List <String > extensions ) {
244
158
String extension = splitFilename (file ).extension ;
245
159
return extensions .contains (extension .toLowerCase ());
@@ -364,21 +278,4 @@ public static List<File> listFiles(File folder, boolean recursive,
364
278
return result ;
365
279
}
366
280
367
- public static File newFile (File parent , String ... parts ) {
368
- File result = parent ;
369
- for (String part : parts ) {
370
- result = new File (result , part );
371
- }
372
-
373
- return result ;
374
- }
375
-
376
- public static boolean deleteIfExists (File file ) {
377
- if (file == null ) {
378
- return true ;
379
- }
380
-
381
- return file .delete ();
382
- }
383
-
384
281
}
0 commit comments