如何将我的文件从一个目录复制到另一个目录?

Gan*_*esh 8 directory android file

我在Android上工作.我的要求是我有一个目录和一些文件,后来我将一些其他文件下载到另一个目录中,我的目的是将最新目录中的所有文件复制到第一个目录中.在将文件从最新版本复制到第一个目录之前,我需要从第一个目录中删除所有文件.

小智 23

    void copyFile(File src, File dst) throws IOException {
       FileChannel inChannel = new FileInputStream(src).getChannel();
       FileChannel outChannel = new FileOutputStream(dst).getChannel();
       try {
          inChannel.transferTo(0, inChannel.size(), outChannel);
       } finally {
          if (inChannel != null)
             inChannel.close();
          if (outChannel != null)
             outChannel.close();
       }
    }
Run Code Online (Sandbox Code Playgroud)

我不记得我在哪里发现了这个,但它来自我用来备份SQLite数据库的有用文章.


Shi*_*til 5

Apache FileUtils非常简单而且非常好.

包括Apache commons io package add commons-io.jar

要么

commons-io android gradle依赖

 compile 'commons-io:commons-io:2.4'
Run Code Online (Sandbox Code Playgroud)

添加此代码

String sourcePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TongueTwister/sourceFile.3gp";
        File source = new File(sourcePath);

        String destinationPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TongueTwister/destFile.3gp";
        File destination = new File(destinationPath);
        try 
        {
            FileUtils.copyFile(source, destination);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)