江西广告网

标题: 拷贝文件到指定目录的程序代码 [打印本页]

作者: 引章小姐     时间: 2009-2-17 10:57
标题: 拷贝文件到指定目录的程序代码
/** * 拷贝一个目录或者文件到指定路径下 * * @param source * @param target */ public static void copy(File source, File target) { File tarpath = new File(target, source.getName()); if (source.isDirectory()) { tarpath.mkdir(); File[] dir = source.listFiles(); for (int i = 0; i < dir.length; i ) { copy(dir[i], 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(); } } }




欢迎光临 江西广告网 (http://bbs.jxadw.com/) Powered by Discuz! X3.2