1473
|
1 package goodjava.io;
|
|
2
|
|
3 import java.io.File;
|
1493
|
4 import java.io.InputStream;
|
|
5 import java.io.OutputStream;
|
1473
|
6 import java.io.IOException;
|
|
7 import java.nio.file.Files;
|
|
8
|
|
9
|
|
10 public final class IoUtils {
|
|
11 private IoUtils() {} // never
|
|
12
|
|
13 public static void move( File from, File to ) throws IOException {
|
|
14 Files.move( from.toPath(), to.toPath() );
|
|
15 }
|
|
16
|
|
17 public static void delete(File file) throws IOException {
|
|
18 Files.deleteIfExists( file.toPath() );
|
|
19 }
|
1475
|
20
|
1501
|
21 public static void mkdirs(File file) throws IOException {
|
|
22 Files.createDirectories( file.toPath() );
|
|
23 }
|
|
24
|
1475
|
25 public static boolean isSymbolicLink(File file) {
|
|
26 return Files.isSymbolicLink(file.toPath());
|
|
27 }
|
|
28
|
|
29 public static void deleteRecursively(File file) throws IOException {
|
|
30 if( file.isDirectory() && !isSymbolicLink(file) ) {
|
|
31 for( File f : file.listFiles() ) {
|
|
32 deleteRecursively(f);
|
|
33 }
|
|
34 }
|
|
35 delete(file);
|
|
36 }
|
|
37
|
1497
|
38 public static void link(File existing,File link) throws IOException {
|
|
39 Files.createLink( link.toPath(), existing.toPath() );
|
|
40 }
|
|
41
|
|
42 public static void symlink(File existing,File link) throws IOException {
|
|
43 Files.createSymbolicLink( link.toPath(), existing.toPath() );
|
1488
|
44 }
|
|
45
|
1494
|
46 public static void copyAll(InputStream in,OutputStream out)
|
1493
|
47 throws IOException
|
|
48 {
|
|
49 byte[] a = new byte[8192];
|
|
50 int n;
|
|
51 while( (n=in.read(a)) != -1 ) {
|
|
52 out.write(a,0,n);
|
|
53 }
|
1499
|
54 in.close();
|
1493
|
55 }
|
|
56
|
1473
|
57 } |