package com.baojiaren.service;
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
File src=new File("F:\\bjr");
File dest=new File("F:\\APP\\");
copyFolder(src,dest);
}
public static void copy(String srcPathStr, String desPathStr)
{
String newFileName = srcPathStr.substring(srcPathStr.lastIndexOf("\\")+1);
System.out.println("源文件:"+newFileName);
desPathStr = desPathStr + File.separator + newFileName;
System.out.println("目标文件地址:"+desPathStr);
try
{
FileInputStream fis = new FileInputStream(srcPathStr);
FileOutputStream fos = new FileOutputStream(desPathStr);
byte datas[] = new byte[1024*8];
int len = 0;
while((len = fis.read(datas))!=-1)
{
fos.write(datas,0,len);
}
fis.close();
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void copyFolder(File src, File dest)
throws IOException {
if(src.isDirectory()){
if(!dest.exists()){
dest.mkdir();
System.out.println("Directory copied from "
+ src + " to " + dest);
}
String files[] = src.list();
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
copyFolder(srcFile,destFile);
}
}else{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("File copied from " + src + " to " + dest);
}
}
}
|