回答
這個例子的改進版本:
// 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