急求:JAVA编写复制文件夹的代码 20

JAVA编写程序实现复制文件夹的功能,最好用流编写,变成高人帮帮忙!!!万分感谢!!我急需啊!!... JAVA编写程序实现复制文件夹的功能,最好用流编写, 变成高人帮帮忙!!!万分感谢!!我急需啊!! 展开
 我来答
ajax_2003
2008-12-12 · TA获得超过4995个赞
知道大有可为答主
回答量:2628
采纳率:0%
帮助的人:2448万
展开全部
一个简单的方式就是调用cmd命令,使用windows自带的功能来替你完成这个功能
我给你写个例子
import java.io.*;
public class test{
public static void main(String[] args){
BufferedReader in = null;
try{
// 这里你就当作操作对dos一样好了 不过cmd /c 一定不要动
Process pro = Runtime.getRuntime().exec("cmd /c copy d:\\ReadMe.txt e:\\");
in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
String str;
while((str = in.readLine()) != null){
System.out.println(str);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(in != null){
try{
in.close();
}catch(IOException i){
i.printStackTrace();
}
}
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
minuses
2008-12-12 · TA获得超过2583个赞
知道小有建树答主
回答量:208
采纳率:0%
帮助的人:246万
展开全部
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

public static void copyDirectory(File srcDir, File destDir) throws IOException {
copyDirectory(srcDir, destDir, true);
}

public static void copyDirectory(File srcDir, File destDir, boolean preserveFileDate) throws IOException {
copyDirectory(srcDir, destDir, null, preserveFileDate);
}

public static void copyDirectory(File srcDir, File destDir, FileFilter filter) throws IOException {
copyDirectory(srcDir, destDir, filter, true);
}

public static void copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate) throws IOException {
if (srcDir == null) {
throw new NullPointerException("Source must not be null");
}
if (destDir == null) {
throw new NullPointerException("Destination must not be null");
}
if (srcDir.exists() == false) {
throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
}
if (srcDir.isDirectory() == false) {
throw new IOException("Source '" + srcDir + "' exists but is not a directory");
}
if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath())) {
throw new IOException("Source '" + srcDir + "' and destination '" + destDir + "' are the same");
}

// Cater for destination being directory within the source directory (see IO-141)
List exclusionList = null;
if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
if (srcFiles != null && srcFiles.length > 0) {
exclusionList = new ArrayList(srcFiles.length);
for (int i = 0; i < srcFiles.length; i++) {
File copiedFile = new File(destDir, srcFiles[i].getName());
exclusionList.add(copiedFile.getCanonicalPath());
}
}
}
doCopyDirectory(srcDir, destDir, filter, preserveFileDate, exclusionList);
}

private static void doCopyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate, List exclusionList) throws IOException {
if (destDir.exists()) {
if (destDir.isDirectory() == false) {
throw new IOException("Destination '" + destDir + "' exists but is not a directory");
}
} else {
if (destDir.mkdirs() == false) {
throw new IOException("Destination '" + destDir + "' directory cannot be created");
}
if (preserveFileDate) {
destDir.setLastModified(srcDir.lastModified());
}
}
if (destDir.canWrite() == false) {
throw new IOException("Destination '" + destDir + "' cannot be written to");
}
// recurse
File[] files = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
if (files == null) { // null if security restricted
throw new IOException("Failed to list contents of " + srcDir);
}
for (int i = 0; i < files.length; i++) {
File copiedFile = new File(destDir, files[i].getName());
if (exclusionList == null || !exclusionList.contains(files[i].getCanonicalPath())) {
if (files[i].isDirectory()) {
doCopyDirectory(files[i], copiedFile, filter, preserveFileDate, exclusionList);
} else {
doCopyFile(files[i], copiedFile, preserveFileDate);
}
}
}
}

private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
if (destFile.exists() && destFile.isDirectory()) {
throw new IOException("Destination '" + destFile + "' exists but is a directory");
}

FileInputStream input = new FileInputStream(srcFile);
try {
FileOutputStream output = new FileOutputStream(destFile);
try {
copy(input, output);
} finally {
try {
output.close();
} catch (IOException ioe) {
// do nothing
}
}
} finally {
try {
input.close();
} catch (IOException ioe) {
// do nothing
}
}

if (srcFile.length() != destFile.length()) {
throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
}
if (preserveFileDate) {
destFile.setLastModified(srcFile.lastModified());
}
}

public static int copy(InputStream input, OutputStream output) throws IOException {
long count = copyLarge(input, output);
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int) count;
}

public static long copyLarge(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}

}

参考资料: http://commons.apache.org/io/

本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式