Here you can find the source of CopyDir(String sourcedir, String destdir)
Parameter | Description |
---|---|
String destdir | the target directory |
public static void CopyDir(String sourcedir, String destdir) throws Exception
//package com.java2s; import java.io.*; public class Main { /**/* w w w . j a va2 s .c om*/ * This class copies an input files of a directory to another directory not include subdir * * @param String sourcedir the directory to copy from such as:/home/bqlr/images * @param String destdir the target directory */ public static void CopyDir(String sourcedir, String destdir) throws Exception { File dest = new File(destdir); File source = new File(sourcedir); String[] files = source.list(); try { makehome(destdir); } catch (Exception ex) { throw new Exception("CopyDir:" + ex.getMessage()); } for (int i = 0; i < files.length; i++) { String sourcefile = source + File.separator + files[i]; String destfile = dest + File.separator + files[i]; File temp = new File(sourcefile); if (temp.isFile()) { try { copy(sourcefile, destfile); } catch (Exception ex) { throw new Exception("CopyDir:" + ex.getMessage()); } } } } /** * create a directory * @param home * @throws Exception */ public static void makehome(String home) throws Exception { File homedir = new File(home); if (!homedir.exists()) { try { homedir.mkdirs(); } catch (Exception ex) { throw new Exception("Can not mkdir :" + home + " Maybe include special charactor!"); } } } /** * This class copies an input file to output file * * @param String input file to copy from * @param String output file */ public static boolean copy(String input, String output) throws Exception { int BUFSIZE = 65536; FileInputStream fis = new FileInputStream(input); FileOutputStream fos = new FileOutputStream(output); try { int s; byte[] buf = new byte[BUFSIZE]; while ((s = fis.read(buf)) > -1) { fos.write(buf, 0, s); } } catch (Exception ex) { throw new Exception("makehome" + ex.getMessage()); } finally { fis.close(); fos.close(); } return true; } }