移动文件时出现异常

时间:2013-05-03 12:08:25

标签: java exception file-io nio filevisitor

我假设这与我对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下的行(但我非常喜欢)感谢任何关于如何解决这个问题的意见。我不明白为什么会引发异常。我猜这是因为文件已被移动。如果是这样的话,我该如何阻止我的代码再次尝试移动它,那么我现在这样做的方式或多或少是正确的?

1 个答案:

答案 0 :(得分:0)

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

在清单中设置正确的权限

  <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());
 }
}