java--基础---复制文件夹及其中的文件
2016-08-14 12:21
260 查看
代码:
package com.io.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class copyDir {
public static void main(String[] args) {
copyDir("d:/a","d:/b");
}
/**
* 复制文件夹
*/
private static void copyDir(String srcRoot,String srcDir,String destDir){
if(srcRoot == null){
srcRoot = srcDir ;
}
//源文件夹
File srcFile = new File(srcDir);
//目标文件夹
File destFile = new File(destDir);
//判断srcFile有效性
if(srcFile == null || !srcFile.exists()){
return ;
}
//创建目标文件夹
if(!destFile.exists()){
destFile.mkdirs();
}
//判断是否文件?
if(srcFile.isFile()){
String absPath = srcFile.getAbsolutePath() ;
//取出上级目录 d:
String parentDir = new File(srcRoot).getParent();
//取出相对的路径
String relPath = absPath.substring(parentDir.length());
File destFile2 = new File(destDir,relPath);
//拷贝文件
copyFile(srcRoot,srcFile.getAbsolutePath(),destDir);
}
//目录
else{
File[] children = srcFile.listFiles();
if(children != null){
for(File f : children){
copyDir(srcRoot,f.getAbsolutePath(),destDir);
}
}
}
}
public static void copyDir(String srcDir,String destDir){
copyDir(srcDir,srcDir,destDir);
<
4000
/span>}
/**
* 复制文件
*/
public static void copyFile(String srcRoot,String path,String destDir){
try {
//准备目录
//取出相对的路径
String tmp = path.substring(srcRoot.length());
String folder = new File(destDir,tmp).getParentFile().getAbsolutePath();
System.out.println(folder);
File destFolder = new File(folder);
destFolder.mkdirs();
File f = new File(path);
//fis
FileInputStream fis = new FileInputStream(path);
String newDestpath = null ;
//文件输出流
FileOutputStream fos = new FileOutputStream(new File(destFolder,new File(path).getName()));
//流的对拷贝
byte[] buf = new byte[1024] ;
int len = 0 ;
while((len = fis.read(buf)) != -1){
fos.write(buf, 0, len);
}
fis.close();
fos.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
package com.io.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class copyDir {
public static void main(String[] args) {
copyDir("d:/a","d:/b");
}
/**
* 复制文件夹
*/
private static void copyDir(String srcRoot,String srcDir,String destDir){
if(srcRoot == null){
srcRoot = srcDir ;
}
//源文件夹
File srcFile = new File(srcDir);
//目标文件夹
File destFile = new File(destDir);
//判断srcFile有效性
if(srcFile == null || !srcFile.exists()){
return ;
}
//创建目标文件夹
if(!destFile.exists()){
destFile.mkdirs();
}
//判断是否文件?
if(srcFile.isFile()){
String absPath = srcFile.getAbsolutePath() ;
//取出上级目录 d:
String parentDir = new File(srcRoot).getParent();
//取出相对的路径
String relPath = absPath.substring(parentDir.length());
File destFile2 = new File(destDir,relPath);
//拷贝文件
copyFile(srcRoot,srcFile.getAbsolutePath(),destDir);
}
//目录
else{
File[] children = srcFile.listFiles();
if(children != null){
for(File f : children){
copyDir(srcRoot,f.getAbsolutePath(),destDir);
}
}
}
}
public static void copyDir(String srcDir,String destDir){
copyDir(srcDir,srcDir,destDir);
<
4000
/span>}
/**
* 复制文件
*/
public static void copyFile(String srcRoot,String path,String destDir){
try {
//准备目录
//取出相对的路径
String tmp = path.substring(srcRoot.length());
String folder = new File(destDir,tmp).getParentFile().getAbsolutePath();
System.out.println(folder);
File destFolder = new File(folder);
destFolder.mkdirs();
File f = new File(path);
//fis
FileInputStream fis = new FileInputStream(path);
String newDestpath = null ;
//文件输出流
FileOutputStream fos = new FileOutputStream(new File(destFolder,new File(path).getName()));
//流的对拷贝
byte[] buf = new byte[1024] ;
int len = 0 ;
while((len = fis.read(buf)) != -1){
fos.write(buf, 0, len);
}
fis.close();
fos.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
相关文章推荐
- JAVA基础初探(十二)Map接口及其常用实现类(HashMap)、File类详解(概述、创建、删除、重命名、文件属性读取/设置、遍历文件夹)
- File文件基本操作之一: java 复制文件夹及其下面的子文件夹
- java基础—IO流——复制一个文件到当前文件夹中
- Java基础之IO流判断文件夹或文件是否存在及其如何创建?
- java 文件夹及其子目录 子文件的复制
- java基础之文件(夹)复制到另一个文件夹
- Java中实现复制文件或文件夹
- java复制文件、文件夹
- Java中实现复制文件或文件夹
- 文件操作之复制文件夹及其中内容
- Java中实现复制文件或文件夹——CopyUtil.java
- Java 写文件,读文件,复制文件,复制文件夹
- java 的 文件、文件夹 的建立、删除、复制以及移动等功能 操作
- 纯Java文件操作工具,支持文件、文件夹的复制、删除、移动
- android-Java中实现复制文件或文件夹 - 随心
- java 实现文件/文件夹复制、删除、移动(一)
- java遍历文件夹并复制文件到指定目录
- Java IO复制个文件夹和里面的全部文件
- php删除 复制文件夹及其文件夹下所有文件
- java实现文件和文件夹的复制