Here you can find the source of copyTree(File srcFile, File trgFile)
Parameter | Description |
---|---|
srcFile | the source tree |
trgFile | the target point where to copy the source tree |
Parameter | Description |
---|---|
IOException | if an I/O error occurs |
public static void copyTree(File srcFile, File trgFile) throws IOException
//package com.java2s; /*/* w w w . j a v a 2 s .c o m*/ * Copyright (c) 2008-2011 by Bjoern Kolbeck, Jan Stender, * Felix Langner, Zuse Institute Berlin * * Licensed under the BSD License, see LICENSE file for details. * */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { /** * Copies a whole directory tree to another directory. * * @param srcFile * the source tree * @param trgFile * the target point where to copy the source tree * @throws IOException * if an I/O error occurs */ public static void copyTree(File srcFile, File trgFile) throws IOException { if (srcFile.isDirectory()) { trgFile.mkdir(); for (File file : srcFile.listFiles()) { copyTree(file, new File(trgFile, file.getName())); } } else { FileChannel in = null, out = null; try { in = new FileInputStream(srcFile).getChannel(); out = new FileOutputStream(trgFile).getChannel(); in.transferTo(0, in.size(), out); } finally { if (in != null) in.close(); if (out != null) out.close(); } } } }