在android中复制/粘贴并创建文件夹

时间:2014-12-08 13:16:37

标签: android file directory copy-paste

我想将文件从一个文件夹复制到另一个文件夹(例如:sdcard0 / folder1 / a.text到sdcard0 / folder2)。我在这个网站和其他网站上看到了很多示例代码,但不是它们适用于我。我不知道我的问题在哪里。我还添加了清单文件的权限。 我该怎么办? 我有几种方法,我将它们从copy1命名为copy3。

      //----------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

}

我像这样使用它们:                 String path = Environment.getExternalStorageDirectory()。toString();                 文件f =新文件(路径+" /folder1/a.txt");                 文件f2 =新文件(路径+" / folder2 /");                 尝试{                   COPY1(F 1,F 2);                 }                 catch(例外e){}

以及创建目录:

File root = android.os.Environment.getExternalStorageDirectory(); 
            File dir = new File (root.getAbsolutePath() + "/AAAli");
            if(!dir.exists()) {                                 
                dir.mkdirs(); // build directory
            }

1 个答案:

答案 0 :(得分:0)

 String sourcePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/yourFolder1/yourFile.png";
            File source = new File(sourcePath);

            String destinationPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/yourFolder2/yourFile.png";
            File destination = new File(destinationPath);
            try 
            {
                FileUtils.copyFile(source, destination);
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }