回答
这个例子的改进版本:
// If targetLocation does not exist, it will be created.
public void copyDirectory(File sourceLocation , File targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists() && !targetLocation.mkdirs()) {
throw new IOException("Cannot create dir " + targetLocation.getAbsolutePath());
}
String[] children = sourceLocation.list();
for (int i=0; i<children.length; i++) {
copyDirectory(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
// make sure the directory we plan to store the recording in exists
File directory = targetLocation.getParentFile();
if (directory != null && !directory.exists() && !directory.mkdirs()) {
throw new IOException("Cannot create dir " + directory.getAbsolutePath());
}
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
有一些更好的错误处理,如果传递的目标文件之处在于不存在的目录更好把手。
查看示例here。 SD卡是外部存储器,因此您可以通过getExternalStorageDirectory
访问它。
是的..我知道可以使用getExternalStorageDirectory访问SD卡..但我怎样才能从一个文件夹复制到另一个相同SD卡的文件夹?谢谢 – 2011-04-19 11:19:20
文件源=新文件(Environment.getExternalStorageDirectory(),“sourcedir”); File dest = new File(Environment.getExternalStorageDirectory(),“destDir”);然后使用链接中的代码。 – 2011-04-19 11:23:16
。对不起...我无法理解,因为我是新来的...在链接他已经给一个文件复制到SD卡转换成字节流..但如何复制整个目录? – 2011-04-19 11:45:20
是的,这是可能的,即时通讯在我的代码中使用下面的方法。希望使用全给你: -
public static void copyDirectoryOneLocationToAnotherLocation(File sourceLocation, File targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i = 0; i < sourceLocation.listFiles().length; i++) {
copyDirectoryOneLocationToAnotherLocation(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
要移动的文件或目录,你可以使用File.renameTo(String path)
功能
File oldFile = new File (oldFilePath);
oldFile.renameTo(newFilePath);
这将从源目录中删除文件。 – Ankit 2013-11-14 11:27:51
- 1. 如何将资产文件夹的内容复制到SD卡?
- 2. 在SD卡上制作文件夹android
- 3. 将音频文件从原始文件夹下载到SD卡
- 4. Android - 如何使用DDMS将文件夹从文件系统复制到SD卡?
- 5. 将文件复制到SD卡
- 6. 删除SD卡上的文件夹
- 7. 如何将资产文件夹中的15Mb文件复制到SD卡...?
- 8. 从资产文件夹复制文件到SD卡似乎并不工作
- 9. 如何选择一个文件夹下载文件在SD卡
- 10. 将资产文件复制到SD卡(找不到文件)?
- 11. 防止用户删除SD卡中的文件夹或文件
- 12. 从SD卡删除一个非空文件夹
- 13. 如何将文件从一个文件夹复制到另一个文件夹
- 14. 将图像添加到文件夹中的SD卡
- 15. 保存文件到SD卡
- 16. 将图库文件夹中的Android图像复制到SD卡替代文件夹中
- 17. 写一个文件到SD卡
- 18. 将文件夹从一组文件复制到另一个文件夹
- 19. 从.APK文件到SD卡的压缩
- 20. 将文件夹从一个文件夹复制到一个新文件夹
- 21. 将png文件从一个文件夹复制到另一个
- 22. 将x文件从一个文件夹复制到另一个
- 23. 使用getExternalStorage()获取从SD卡和USB到不同文件夹的文件
- 24. Applescript - 将文件从当前文件夹复制到另一个文件夹
- 25. 将pdf文件从资产复制到SD卡
- 26. 如何将文件从SD卡复制到系统
- 27. 将数据从SD卡复制到原始文件
- 28. 如何在android中的SD卡中制作文件夹
- 29. 在android中创建一个只读文件夹在SD卡中
- 30. 当没有SD卡时,你可以写入SD卡文件夹吗?
当然,它是。 – 2011-04-19 10:42:25
请问该怎么做? – 2011-04-19 10:54:39