Java 移动文件时发生异常
我假设这与我对Java 移动文件时发生异常,java,exception,file-io,nio,filevisitor,Java,Exception,File Io,Nio,Filevisitor,我假设这与我对FileVisitor如何工作和解析目录的有限知识有关。我要做的是将一个目录的内容移动到另一个目录中。我通过如下方式实现FileVisitor: public class Mover implements FileVisitor<Path> { private Path target; private Path source; public Mover(Path source, Path target) { this.targ
FileVisitor
如何工作和解析目录的有限知识有关。我要做的是将一个目录的内容移动到另一个目录中。我通过如下方式实现FileVisitor
:
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;
}
}
Files.walkFileTree(from, new Mover(from, to));
我不喜欢在调用
walkFileTree
时两次添加from
,但目前我的问题主要是代码中TODO
下的行(不过我非常感谢您对如何解决该问题的任何意见)。我不明白为什么会提出这个例外。我猜这是因为文件已经被移动了。如果是这样的话,我该如何阻止我的代码再次尝试移动它,我现在这样做是否或多或少是正确的?下面是一个以编程方式移动文件的函数
在清单中设置正确的权限
要删除文件,请使用
抄袭
谢谢,但这不是一个Android项目,也没有真正回答我的问题。啊…你应该移动postVisitDirectory中的目录,或者跳过visitFile。FileVisitor将运行previsit/foo,然后是/foo/file1.txt,然后是/foo/file2.txt,然后是/foo的postvisit。当它运行previsit/foo时,您正在移动/foo(以及它下面的所有内容)。当它运行/foo/file1.txt时,您已经移动了/foo/file1.txt(因为您移动了/foo)。
<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());
}
}