1

我假设这与我对如何FileVisitor工作和解析目录的知识有限有关。我想要做的是将目录的内容移动到另一个目录中。我通过这样实现来做到这FileVisitor<Path>一点:

public class Mover implements FileVisitor<Path> {

    private Path target;
    private Path source;

    public Mover(Path source, Path target) {
        this.target = target;
        this.source = source;
    }

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        Path targetDir = target.resolve(source.relativize(dir));
        try {
            Files.move(dir, targetDir);
        } catch (FileAlreadyExistsException e) {
            if(!Files.isDirectory(targetDir)) {
                System.out.println("Throwing e!");
                throw e;                
            }
        }
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path file, IOException exc) throws IOException {
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        try {
            Files.move(file, target.resolve(source.relativize(file)));                      
    } catch (NoSuchFileException e) {
                //TODO: Figure out why this exception is raised!
                System.out.println("NoSuchFileException");
            }
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
        return FileVisitResult.CONTINUE;
    }   
}

反过来,我Mover像这样使用我的课程:

Files.walkFileTree(from, new Mover(from, to));

我不喜欢from在调用时添加两次walkFileTree,但目前我的问题主要出TODO在我的代码中的行下(但是我非常感谢有关如何解决该问题的任何评论)。我不明白为什么会引发该异常。我猜这是因为文件已经被移动了。如果是这种情况,我该如何阻止我的代码再次尝试移动它,我现在这样做的方式或多或少是正确的?

4

1 回答 1

-1

下面是一个以编程方式移动文件的函数

在清单中设置正确的权限

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    private void moveFile(String inputPath, String inputFile, String outputPath) {
InputStream in = null;
OutputStream out = null;
try {
    //create output directory if it doesn't exist
    File dir = new File (outputPath); 
    if (!dir.exists())
    {
        dir.mkdirs();
    }
    in = new FileInputStream(inputPath + inputFile);        
    out = new FileOutputStream(outputPath + inputFile);
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    in.close();
    in = null;
        // write the output file
        out.flush();
    out.close();
    out = null;
    // delete the original file
    new File(inputPath + inputFile).delete(); 
} 
     catch (FileNotFoundException fnfe1) {
    Log.e("tag", fnfe1.getMessage());
}
      catch (Exception e) {
    Log.e("tag", e.getMessage());
  }
}

删除文件使用

     private void deleteFile(String inputPath, String inputFile) {
    try {
    // delete the original file
    new File(inputPath + inputFile).delete();  
   }
  catch (FileNotFoundException fnfe1) {
    Log.e("tag", fnfe1.getMessage());
 }
  catch (Exception e) {
    Log.e("tag", e.getMessage());
 }
}

复印

         private void copyFile(String inputPath, String inputFile, String outputPath)         {
InputStream in = null;
OutputStream out = null;
try {
    //create output directory if it doesn't exist
    File dir = new File (outputPath); 
    if (!dir.exists())
    {
        dir.mkdirs();
    }
    in = new FileInputStream(inputPath + inputFile);        
    out = new FileOutputStream(outputPath + inputFile);
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    in.close();
    in = null;
        // write the output file (You have now copied the file)
        out.flush();
    out.close();
    out = null;        
 }  catch (FileNotFoundException fnfe1) {
    Log.e("tag", fnfe1.getMessage());
  }
        catch (Exception e) {
    Log.e("tag", e.getMessage());
 }
}
于 2013-05-03T12:40:57.007 回答