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
|
|
21 public static boolean isSymbolicLink(File file) {
|
|
22 return Files.isSymbolicLink(file.toPath());
|
|
23 }
|
|
24
|
|
25 public static void deleteRecursively(File file) throws IOException {
|
|
26 if( file.isDirectory() && !isSymbolicLink(file) ) {
|
|
27 for( File f : file.listFiles() ) {
|
|
28 deleteRecursively(f);
|
|
29 }
|
|
30 }
|
|
31 delete(file);
|
|
32 }
|
|
33
|
1488
|
34 public static void link(File from,File to) throws IOException {
|
|
35 Files.createLink( to.toPath(), from.toPath() );
|
|
36 }
|
|
37
|
1493
|
38 public static long copyAll(InputStream in,OutputStream out)
|
|
39 throws IOException
|
|
40 {
|
|
41 long total = 0;
|
|
42 byte[] a = new byte[8192];
|
|
43 int n;
|
|
44 while( (n=in.read(a)) != -1 ) {
|
|
45 out.write(a,0,n);
|
|
46 total += n;
|
|
47 }
|
|
48 return total;
|
|
49 }
|
|
50
|
1473
|
51 } |