本帖最后由 郑苑东 于 2012-4-7 23:53 编辑
改动后的。。还有仔细看看我的代码。。你代码逻辑错误了。。。。
package com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Demo3 {
public static void main(String[] args) throws IOException {
File f1 = new File("D:\\ppppppppppppppp");
File f2 = new File("D:\\o");
copy(f1, f2);
}
public static void copy(File f1, File f2) throws IOException {
if (f1 == null)
return;
String[] filenames = f1.list();
for (int i = 0; i < filenames.length; i++) {
System.out.println(filenames);
File fl1 = new File(f1, filenames);
// ------------------------->这句提示出错....纠结,实在想不通哪里错了
File fl2 = new File(f2, fl1.getName());
if (fl1.isDirectory()) {
fl2.mkdir();
copy(fl1, fl2); // 这个地方错了。。而且害我建了两个不能删除的文件夹。。自己也要小心点。。你原来的死循环了。。一直传入f1.。。
}
else {
copyFile(fl1, fl2); ///这个地方也错了。。
}
}
}
public static void copyFile(File f1, File f2) {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(f1);
out = new FileOutputStream(f2);
readAndwriter(in, out);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
ioClose(in, out);
}
}
private static void readAndwriter(InputStream in, OutputStream out) throws IOException {
byte[] byts = new byte[1024];
int len;
while ((len = in.read(byts)) != -1) {
out.write(byts, 0, len);
}
}
private static void ioClose(InputStream in, OutputStream out) {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
你的代码让我栈溢出两次啊 。。。加引号 把所有的加入大夸号中。。。 |