62,621
社区成员
发帖
与我相关
我的任务
分享package copyfileordirectory;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author Administrator
*/
public class CopyFileOrDirectory {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String from = null;
String to = null;
try {
System.out.println("请输入已存在的源文件夹或源文件的路径:");
from = reader.readLine();
System.out.println("请输入目标文件夹或目标文件的路径:");
to = reader.readLine();
File sourceDir = new File(from);
File objectDir = new File(to);
File[] directory = sourceDir.listFiles();
for (File dir : directory) {
if (dir.isFile()) {
copyFile(dir.getAbsolutePath(), to + "/" + dir.getName());
}
if (dir.isDirectory()) {
copyDirectory(from+"/"+dir.getName(),to+"/"+dir.getName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFile(String from, String to) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(from));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(to));
int length = 0;
byte[] b = new byte[1024];
while ((length = in.read()) != -1) {
out.write(b, 0, length);
}
in.close();
out.close();
}
public static void copyDirectory(String from, String to) throws IOException {
File sourceDir = new File(from);
File objectDir = new File(to);
File[] directory = sourceDir.listFiles();
for (File dir : directory) {
if (dir.isFile()) {
copyFile( dir.getAbsolutePath(), new File(to).getAbsolutePath() + "/" + dir.getName());
}
if (dir.isDirectory()) {
String fromdir=from+"/"+dir.getName();
String todir=to+"/"+dir.getName();
copyDirectory(fromdir,todir);
}
}
}
}public static void copyFileByName(String sourcePathname, String newPathname) {
File sourceFile = new File(sourcePathname);
File newFile = new File(newPathname + File.separator
+ sourceFile.getName());
copyFileByFile(sourceFile, newFile, false);
}
/**
*
* @param sourceFile
* @param newFile
* @param flag
* false复制,true剪切
*/
public static void copyFileByFile(File sourceFile, File newFile,
boolean flag) {
if (sourceFile.isFile()) {
copyFileByStream(sourceFile, newFile);
} else {
newFile.mkdir();
for (File file : sourceFile.listFiles()) {
copyFileByFile(file, new File(newFile.getPath()
+ File.separator + file.getName()), flag);
}
}
if (flag) {
sourceFile.delete();
}
}
/**
* 复制文件
*
* @param sourceFile
* @param newFile
*/
public static void copyFileByStream(File sourceFile, File newFile) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(sourceFile);
fos = new FileOutputStream(newFile);
writeRead(fos, fis);
} catch (Exception e) {
throw new UtilException(e.getMessage());
} finally {
closeInputStream(fis);
closeOutputStream(fos);
}
}

package com.study.test;
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 Test {
public static void main(String[] args) throws Exception {
File file = new File("D:\\DOC");
copy(file, "D:\\11");
}
public static void copy(File file, String dir){
File direstory = new File(dir);
direstory.mkdir();
InputStream in = null;
OutputStream out = null;
if(file.isDirectory()){
File[] files = file.listFiles();
for(File f : files){
copy(f, dir + File.separator + file.getName());
}
}else{
try {
in = new FileInputStream(file);
out = new FileOutputStream(dir + File.separator + file.getName());
int i;
byte[] buf = new byte[1024];
while((i = in.read(buf)) != -1){
out.write(buf, 0, i);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
if(out != null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package copyfileordirectory;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author Administrator
*/
public class CopyFileOrDirectory {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String from = null;
String to = null;
try {
System.out.println("请输入已存在的源文件夹或源文件的路径:");
from = reader.readLine();
System.out.println("请输入目标文件夹或目标文件的路径:");
to = reader.readLine();
File sourceDir = new File(from);
File objectDir = new File(to);
objectDir.mkdirs();
File[] directory = sourceDir.listFiles();
for (File dir : directory) {
if (dir.isFile()) {
copyFile(dir.getAbsolutePath(), to + "/" + dir.getName());
}
if (dir.isDirectory()) {
copyDirectory(from+"/"+dir.getName(),to+"/"+dir.getName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFile(String from, String to) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(from));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(to));
int length = 0;
byte[] b = new byte[1024];
while ((length = in.read()) != -1) {
out.write(b, 0, length);
}
in.close();
out.close();
}
public static void copyDirectory(String from, String to) throws IOException {
File sourceDir = new File(from);
File objectDir = new File(to);
objectDir.mkdirs();
File[] directory = sourceDir.listFiles();
for (File dir : directory) {
if (dir.isFile()) {
copyFile( dir.getAbsolutePath(), new File(to).getAbsolutePath() + "/" + dir.getName());
}
if (dir.isDirectory()) {
String fromdir=from+"/"+dir.getName();
String todir=to+"/"+dir.getName();
copyDirectory(fromdir,todir);
}
}
}
}以上是程序的源代码。跪求大神的出现。。。
public static boolean copyDir(String fromDir,String toDir) {
File in=new File(fromDir);
File out=new File(toDir);
if (!in.exists()) {
return false;
}
if (!out.exists()) {
out.mkdirs();
} else {
}
File[] file=in.listFiles();
FileInputStream fis=null;
FileOutputStream fos=null;
for (int i=0;i<file.length;i++) {
if (file[i].isFile()) {
try {
fis=new FileInputStream(file[i]);
File f = new File(toDir+"/"+file[i].getName());
fos=new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int c;
byte[] b=new byte[1024*5];
try {
while((c=fis.read(b))!=-1){
fos.write(b, 0, c);
}
fis.close();
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else {
copyDir(fromDir+"/"+file[i].getName(),toDir+"/"+file[i].getName());
}
}
return true;
}