하위 디렉토리를 포함하여 디렉토리에있는 모든 파일을 복사하는 효율적인 방법입니까? 무한 재귀의 기회가 있습니까? 내가 바꿔야 할 것이 있습니까? 나는 그것이 작동하는 것을 안다. 그러나 나는 이것을하기위한 더 쉬운 방법이 있어야한다고 생각한다.복사 파일의 효율적인 방법입니까
private void copy(File file, String path) {
String fileName = file.getPath();
System.out.println(fileName);
fileName = fileName.substring(fileName.lastIndexOf("\\"));
if (path == null)
path = Storage.getStorageDirectoryPath();
File toWrite = new File(path + File.separator + fileName);
if (file.isDirectory()) {
toWrite.mkdir();
File inDirectory[] = file.listFiles();
for (File f : inDirectory)
copy(f, toWrite.getPath());
} else {
try {
InputStream inStream = new FileInputStream(file);
OutputStream outStream = new FileOutputStream(toWrite);
byte buffer[] = new byte[1024];
int length = 0;
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
감사
효율적이라고 생각합니다. – kosa
사용중인 Java 버전은 무엇입니까? Java 7은 이러한 목적을 위해'Files' 클래스를 제공합니다. – Bernard
폴더에서 이전 폴더로 심볼릭 링크가 발생하면 무한 루프가 발생할 것입니다. – kba