2013-02-28 31 views
8

Nio sınıfında yeniyim ve bir dosya dizini yeni oluşturulan dizine taşıma konusunda sorun yaşıyorum.Java: Directory'yi taşımak için nio Files.copy'yi kullanma

Ben ilk 2 dizinleri oluşturun:

File sourceDir = new File(sourceDirStr); //this directory already exists 
File destDir = new File(destDirectoryStr); //this is a new directory 

Sonra kullanarak, yeni dizine varolan dosyaları kopyalamak için deneyin:

Exception in thread "main" java.nio.file.FileSystemException: destDir/Experiment.log: Not a directory 
:

Path destPath = destDir.toPath(); 
for (int i = 0; i < sourceSize; i++) { 
    Path sourcePath = sourceDir.listFiles()[i].toPath(); 
    Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName())); 
} 

Bu, aşağıdaki hata atıyor

destDir/Experiment.log'un varolan bir dizin olmadığını biliyorum; Files.copy işleminin sonucu olarak yeni bir dosya olmalıdır. Birisi operasyonumun yanlış gittiğine işaret edebilir mi? Teşekkürler!

+2

uyarlanmıştır? Eğer değilse, önce ['File # mkdirs()'] kullanarak oluşturmanız gerekebilir (http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs()) . – millimoose

+0

'True''i döndüren' destDir.exists() 'ı çalıştırdım. Neredeyse, destDir/Experiment.log'un bir dizin olması gerektiğini düşünüyor. Durum böyle değil mi? –

cevap

12

Sen dizinleri kopyalamak için walkFileTree kullanmak gerekir. Bir dizinde Files.copy kullanırsanız, sadece boş bir dizin oluşturulur.

sonra kodu alınan/`destDir` da disk üzerinde var mı http://codingjunkie.net/java-7-copy-move/

File src = new File("c:\\temp\\srctest"); 
File dest = new File("c:\\temp\\desttest"); 
Path srcPath = src.toPath(); 
Path destPath = dest.toPath(); 

Files.walkFileTree(srcPath, new CopyDirVisitor(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING)); 

public static class CopyDirVisitor extends SimpleFileVisitor<Path> 
{ 
    private final Path fromPath; 
    private final Path toPath; 
    private final CopyOption copyOption; 

    public CopyDirVisitor(Path fromPath, Path toPath, CopyOption copyOption) 
    { 
     this.fromPath = fromPath; 
     this.toPath = toPath; 
     this.copyOption = copyOption; 
    } 

    @Override 
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException 
    { 
     Path targetPath = toPath.resolve(fromPath.relativize(dir)); 
     if(!Files.exists(targetPath)) 
     { 
      Files.createDirectory(targetPath); 
     } 
     return FileVisitResult.CONTINUE; 
    } 

    @Override 
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException 
    { 
     Files.copy(file, toPath.resolve(fromPath.relativize(file)), copyOption); 
     return FileVisitResult.CONTINUE; 
    } 
} 
4

Sadece mevcut değilse hedef dizini yapın. sourceDir.listFiles() ayrıca t içine recurse ya görmezden istiyorum ya olacak dizinleri, döneceği

File sourceDir = new File(source); //this directory already exists 
File destDir = new File(dest); //this is a new directory 
destDir.mkdirs(); // make sure that the dest directory exists 

Path destPath = destDir.toPath(); 
for (File sourceFile : sourceDir.listFiles()) { 
    Path sourcePath = sourceFile.toPath(); 
    Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName())); 
} 

Not ...

+0

Teşekkürler, ama maalesef hala aynı hatayı atıyor. Hatta 'True' döndüren' destDir.exists() 'ı çalıştırdım. Neredeyse, destDir/Experiment.log'un bir dizin olması gerektiğini düşünüyor. Durum böyle değil mi? –

+0

Kodu yan yana çalıştırdım ve dizin içinde kopyalamaya çalıştığım alt dizin bulunmadığı sürece çalışıyor ... – RudolphEst

+0

Düşünebildiğim tek fark şudur; = 0; i

0
for (int i = 0; i < sourceSize; i++) { 
    Path sourcePath = sourceDir.listFiles()[i].toPath(); 
    Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName())); 
} 

Bu çok garip bir koddur. Zaten sourceSize'da bir yerden bir dosya sayınız var, ancak her yineleme için listFiles()'u arıyorsunuz. Daha böyle bir şey beklerdik:

for (File file : sourceDir.listFiles()) { 
    Path sourcePath = file.toPath(); 
    Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName())); 
} 
+0

Bu sorunun kapsamı dışında kalan nedenlerden dolayı 'int i = 0' ... biçimini kullanmam gerekiyor. Rastgele sayıları seçmek ve indeks numarasından geçmekle ilgilidir. –

+1

Noktayı tamamen kaçırdınız. Bu, her yinelemede listFiles() öğesini çağırmaya devam etmenin bir nedeni değildir. Yineleme ve aynı dosyayı iki kez işleme arasında dizin değiştirme riskini çalıştırıyorsunuz. – EJP

İlgili konular