求大神指點:這個拷貝目錄的的代碼雖然能把檔案都拷貝但會發生運行錯誤,請問是具體的錯誤是什么?
我覺得應該是遞回引起的錯誤,求能人指點指點。
import java.io.*;
public class CopyDir {
public static void copyDirector(File needCopy,File putPath){
String name = needCopy.getName(); //目錄名字
File[] sub = needCopy.listFiles(); //目錄下的子檔案
String path = putPath.getAbsolutePath() ;//目標的絕對路徑
File makePath = new File(path+"\\"+name); //需要被創造的目錄
makePath.mkdir();//創造目錄
String newPath = makePath.getAbsolutePath(); //被創造目錄的絕對路徑
if(sub.length == 0){
return;
}
FileInputStream a1 = null;
FileOutputStream b2 = null;
for(File temp : sub){
String tempName = temp.getName(); //子檔案的名字
if(temp.isDirectory()){
copyDirector(temp,makePath);
}
try {
a1 = new FileInputStream(temp);
b2 = new FileOutputStream(newPath+"\\"+tempName);
byte[] x = new byte[1024*1024];
int readCound = 0;
while ((readCound=a1.read(x)) != -1){
b2.write(x,0,readCound);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
a1.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
b2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
錯誤資訊:
java.io.FileNotFoundException: 檔案的絕對路徑 (拒絕訪問。)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:212)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:154)
at com.LiuZeShen.JavaSE.Copy.CopyDir.copyDirector(CopyDir.java:29)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/202691.html
標籤:Java SE
