0
Java의 내용으로 전체 디렉토리를 삭제하려면 어떻게해야합니까? 몇 가지 코드를 시도했지만 작동하지 않습니다.디렉토리를 재귀 적으로 삭제 java가 작동하지 않음
D : \ MAINDIR \ CHANTIER : Erreur 드 강의
public static void removeRecursive(Path path) throws IOException
{
Files.walkFileTree(path, new SimpleFileVisitor<Path>()
{
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException
{
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException
{
// try to delete the file anyway, even if its attributes
// could not be read, since delete-only access is
// theoretically possible
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException
{
if (exc == null)
{
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
else
{
// directory iteration failed; propagate exception
throw exc;
}
}
});
}
그것은 오류로 발생합니다. 범위를 벗어난 문자열 색인 : 여기 -1
그리고 내 경로 D입니다 : \ MAINDIR \ 채널 \ 부지 \ 서류 \ mail.zip
내가 디렉토리 채널의 \의 부지를 삭제할 \ 서류 \ 그 내용 와 mail.zip하지만 내 기능은 \ 서류 \의 mail.zip 제거하고 오류가
시
언급 던졌습니다 : I는 입력 파일 D로 제공 : \ MAINDIR \ 채널 사전 에서감사합니다
전체 스택 트레이스를 추가하십시오 – Jens
'FileVisitor'는 좋은 선택이 아니라고 생각합니다. 몇 가지 간단한 해결책은 [이 질문] (http://stackoverflow.com/questions/20281835/how-to-delete-a-folder-with-files-using-java)을 확인하십시오. – ArcticLord
@jens 이것이 보여주는 유일한 오류입니다 –