Я хочу скопировать файл из одной папки в другую папку (например: sdcard0/folder1/a.text в sdcard0/folder2). Я видел много примеров кода на этом сайте и других сайтах, но не для них работает для меня. Я не знаю, где моя проблема. Я также добавляю разрешение на файл манифеста. Что мне делать? У меня есть несколько методов для этого, что я назвал их от copy1 до copy3.Копирование/вставка и создание папки в android
//----------Method 1
public void copy1(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
//----------Method 2
public void copy2(File src, File dst) throws IOException {
FileInputStream inStream = new FileInputStream(src);
FileOutputStream outStream = new FileOutputStream(dst);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
}
//----------Method 3
public static boolean copy3(File source, File dest){
try{
// Declaration et ouverture des flux
java.io.FileInputStream sourceFile = new java.io.FileInputStream(source);
try{
java.io.FileOutputStream destinationFile = null;
try{
destinationFile = new FileOutputStream(dest);
// Lecture par segment de 0.5Mo
byte buffer[] = new byte[512 * 1024];
int nbLecture;
while ((nbLecture = sourceFile.read(buffer)) != -1){
destinationFile.write(buffer, 0, nbLecture);
}
} finally {
destinationFile.close();
}
} finally {
sourceFile.close();
}
} catch (IOException e){
e.printStackTrace();
return false; // Erreur
}
return true; // Résultat OK
}
и я использую их, как это: Строка путь = Environment.getExternalStorageDirectory() ToString();. Файл f = новый Файл (путь + "/ folder1/a.txt"); Файл f2 = новый файл (путь + "/ folder2 /"); try { copy1 (f, f2); } поймать (Exception е) {}
, а также для создания каталога:
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/AAAli");
if(!dir.exists()) {
dir.mkdirs(); // build directory
}
показать код, который вы пытались –
Помоги мне. что мне делать? – Khodayar
проверить мой ответ –