|
| 1 | +package com.sandwich.util; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.OutputStream; |
| 9 | + |
| 10 | +public class FileUtils { |
| 11 | + |
| 12 | + public static void copy(String fileName0, String fileName1) throws IOException{ |
| 13 | + copy(new File(fileName0), new File(fileName1)); |
| 14 | + } |
| 15 | + |
| 16 | + public static void copy(File file0, File file1) throws IOException{ |
| 17 | + if((file0 == null || !file0.exists()) && file1 == null){ |
| 18 | + throw new IllegalArgumentException("Both path's must actually exist"); |
| 19 | + } |
| 20 | + if (file0.isDirectory()) { |
| 21 | + if (!file1.exists()) { |
| 22 | + file1.mkdir(); |
| 23 | + } |
| 24 | + String files[] = file0.list(); |
| 25 | + for (int i = 0; i < files.length; i++) { |
| 26 | + copy(new File(file0, files[i]), new File(file1, files[i])); |
| 27 | + } |
| 28 | + } else { |
| 29 | + if (!file0.exists()) { |
| 30 | + System.out.println("File or directory does not exist: "+file0); |
| 31 | + System.exit(0); |
| 32 | + } else { |
| 33 | + InputStream in = new FileInputStream(file0); |
| 34 | + OutputStream out = new FileOutputStream(file1); |
| 35 | + |
| 36 | + // Transfer bytes from in to out |
| 37 | + byte[] buf = new byte[1024]; |
| 38 | + int len; |
| 39 | + |
| 40 | + while ((len = in.read(buf)) > 0) { |
| 41 | + out.write(buf, 0, len); |
| 42 | + } |
| 43 | + |
| 44 | + in.close(); |
| 45 | + out.close(); |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | +} |
0 commit comments