Here you can find the source of copyDirectory(File source, File target)
Parameter | Description |
---|---|
source | a parameter |
target | a parameter |
public static void copyDirectory(File source, File target)
//package com.java2s; // %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /**/* w ww . j ava 2 s . c o m*/ * copy the dirictory(include sub folders and files) from the source to the target, the copyed dirictory will be * under the target. * * @param source * @param target */ public static void copyDirectory(File source, File target) { File tarpath = new File(target, source.getName()); if (source.isDirectory()) { tarpath.mkdir(); File[] dir = source.listFiles(); for (File element : dir) { copyDirectory(element, tarpath); } } else { try { InputStream is = new FileInputStream(source); OutputStream os = new FileOutputStream(tarpath); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) != -1) { os.write(buf, 0, len); } is.close(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }