回答
这个例子的改进版本:
// 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. 将文件从android_asset文件夹复制到SD卡
- 2. USB文件夹和SD卡文件夹
- 3. SD卡新建文件夹
- 4. 链接到SD卡中的文件夹
- 5. android - 如何将图像复制到SD卡中的文件夹
- 6. 如何将资产文件夹的内容复制到SD卡?
- 7. 在SD卡上制作文件夹android
- 8. 将音频文件从原始文件夹下载到SD卡
- 9. 在OS级别将文件夹的内容复制到SD卡上的另一个文件夹?
- 10. 从资产到SD卡包含10个mp3文件的复制文件夹
- 11. Android - 如何使用DDMS将文件夹从文件系统复制到SD卡?
- 12. 将文件复制到SD卡
- 13. Android的子文件夹的SD卡
- 14. 如何将res/raw文件夹中的xml文件复制到android的sd卡?
- 15. 将多个pcm文件存储在SD卡上的文件夹
- 16. 从原始文件夹复制图像到外部SD卡?
- 17. 删除SD卡上的文件夹
- 18. 如何将资产文件夹中的15Mb文件复制到SD卡...?
- 19. 文件从SD卡
- 20. 将视频文件保存到SD卡文件夹
- 21. 从资产文件夹复制文件到SD卡似乎并不工作
- 22. 如何选择一个文件夹下载文件在SD卡
- 23. 将资产文件复制到SD卡(找不到文件)?
- 24. 将文件复制到SD卡。 (从一个地方到另一个地方)
- 25. 将图片从用户图库复制到SD卡上的文件夹
- 26. 获取SD卡中的文件夹和文件的列表
- 27. 防止用户删除SD卡中的文件夹或文件
- 28. 从SD卡删除一个非空文件夹
- 29. 如何将图库文件夹中的图像复制到SD卡中的任何文件夹?
- 30. 从SD卡读取文件
当然,它是。 – 2011-04-19 10:42:25
请问该怎么做? – 2011-04-19 10:54:39