日期:2014-05-20 浏览次数:20808 次
public void copyFolder(String oldPath, String newPath) {
try {
new File(newPath).mkdirs();
File a=new File(oldPath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(oldPath.endsWith(File.separator)){
temp=new File(oldPath+file[i]);
}else{
temp=new File(oldPath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" + //问题1:
(temp.getName()).toString());
byte[] b = new byte[1024 * 5]; //问题2:
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
}catch (Exception e) {
message = "复制整个文件夹内容操作出错";
}
}
1. 你是UNIX平台嘛?
2.这里byte[]的大小可以随便设置的吗?(如果read()方法一次性最多能读 1024*5 大小的数据,那么如果这个文件超过 1024*5 的大小怎么办呢? 也就是说,它的内部会修改偏移量,第二次的读取则从偏移量开始咯?那么byte[]的大小为什么不设置的越大越好呢?)
//我觉得是可以随便设置,看文件多大了,合适就好,大了也浪费空间。
下面 While 循环中每次都是读取一个1字节?
//不是的,它是每次都读一个byte数组从0到len长度.
int read(byte[] b) //从此输入流中将最多 b.length 个字节的数据读入一个字节数组