我有这个Android代码用于将文件从源复制到目标:
private void move(File src, File dest) {
try {
InputStream is = new FileInputStream(src);
OutputStream os = new FileOutputStream(new File(
dest.getAbsolutePath() + "/" + src.getName()));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) > 0) {
os.write(buffer, 0, len);
}
is.close();
os.close();
// delete old file
src.delete();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
但是当我打电话
时,我获得了ENOENT(没有suche文件或目录)warnintInputStream is = new FileInputStream(src);
并且没有文件被复制。
这是一个带有src和dst值的日志
src: /storage/emulated/0/DCIM/Camera/IMG_20141026_183702.jpg
dst:/storage/emulated/0/Pictures/destfolder
这是警告信息:
java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20141026_183702.jpg:
open failed: ENOENT (No such file or directory)
路径与src值相同! 怎么了?
答案 0 :(得分:0)
您确定错误来自FileInputStream吗?
FileOutputStream不会创建目录。在打开流之前使用dest.mkdirs()可能会解决您的错误。