Here you can find the source of copyDirectoryContent(File srcPath, File targetPath)
Parameter | Description |
---|---|
srcPath | source directory to copy |
targetPath | target directory which will be created by copy |
true
if copy went OK else false
public static boolean copyDirectoryContent(File srcPath, File targetPath)
//package com.java2s; /*//from www . ja v a 2 s.c om * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). * All rights reserved. * This component and the accompanying materials are made available * under the terms of "Eclipse Public License v1.0" * which accompanies this distribution, and is available * at the URL "http://www.eclipse.org/legal/epl-v10.html". * * Initial Contributors: * Nokia Corporation - Initial contribution * * Contributors: * * Description: This file is part of com.nokia.tools.vct.common.core component. */ 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 { /** * Copy existing directory on srcPath into non-existing targetPath * directory. * * @param srcPath * source directory to copy * @param targetPath * target directory which will be created by copy * @return <code>true</code> if copy went OK else <code>false</code> */ public static boolean copyDirectoryContent(File srcPath, File targetPath) { if (srcPath.isDirectory()) {// directory targetPath.mkdirs(); File[] srcChildFiles = srcPath.listFiles(); boolean ok = true; for (int i = 0; i < srcChildFiles.length; i++) { File srcFile = srcChildFiles[i]; boolean temp = copyDirectoryContent(srcFile, new File(targetPath, srcFile.getName())); if (!temp) { ok = false; } } return ok; } else {// file try { FileInputStream fis = new FileInputStream(srcPath); FileOutputStream fos = new FileOutputStream(targetPath); copyStream(fis, fos); } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } return true; } } /** * Copy stream content. * * The content is read from input stream and is written to output. Streams * are not closed * * @param is * Input stream * @param os * Output stream * @throws IOException */ public static long copyStream(InputStream is, OutputStream os) throws IOException { byte[] buffer = new byte[16384]; int len; long result = 0; while ((len = is.read(buffer)) >= 0) { os.write(buffer, 0, len); result += len; } return result; } }