首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何以编程方式移动、复制和删除SD上的文件和目录?

如何以编程方式移动、复制和删除SD上的文件和目录?
EN

Stack Overflow用户
提问于 2010-11-14 23:34:21
回答 11查看 186.5K关注 0票数 99

我想以编程方式移动,复制和删除SD卡上的文件和目录。我已经在谷歌上搜索过了,但是找不到任何有用的东西。

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2010-11-14 23:38:18

Use standard Java I/O。使用Environment.getExternalStorageDirectory()进入外部存储(在某些设备上是SD卡)的根目录。

票数 28
EN

Stack Overflow用户

发布于 2012-07-04 19:02:27

在清单中设置正确的权限

代码语言:javascript
复制
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

下面是一个将以编程方式移动您的文件的函数

代码语言:javascript
复制
private void moveFile(String inputPath, String inputFile, String outputPath) {

    InputStream in = null;
    OutputStream out = null;
    try {

        //create output directory if it doesn't exist
        File dir = new File (outputPath); 
        if (!dir.exists())
        {
            dir.mkdirs();
        }


        in = new FileInputStream(inputPath + inputFile);        
        out = new FileOutputStream(outputPath + inputFile);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;

            // write the output file
            out.flush();
        out.close();
        out = null;

        // delete the original file
        new File(inputPath + inputFile).delete();  


    } 

         catch (FileNotFoundException fnfe1) {
        Log.e("tag", fnfe1.getMessage());
    }
          catch (Exception e) {
        Log.e("tag", e.getMessage());
    }

}

要删除文件,请使用

代码语言:javascript
复制
private void deleteFile(String inputPath, String inputFile) {
    try {
        // delete the original file
        new File(inputPath + inputFile).delete();  
    }
    catch (Exception e) {
        Log.e("tag", e.getMessage());
    }
}

复制

代码语言:javascript
复制
private void copyFile(String inputPath, String inputFile, String outputPath) {

    InputStream in = null;
    OutputStream out = null;
    try {

        //create output directory if it doesn't exist
        File dir = new File (outputPath); 
        if (!dir.exists())
        {
            dir.mkdirs();
        }


        in = new FileInputStream(inputPath + inputFile);        
        out = new FileOutputStream(outputPath + inputFile);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;

            // write the output file (You have now copied the file)
            out.flush();
        out.close();
        out = null;        

    }  catch (FileNotFoundException fnfe1) {
        Log.e("tag", fnfe1.getMessage());
    }
            catch (Exception e) {
        Log.e("tag", e.getMessage());
    }

}
票数 169
EN

Stack Overflow用户

发布于 2014-07-19 21:58:47

移动文件:

代码语言:javascript
复制
File from = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/kaic1/imagem.jpg");
File to = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/kaic2/imagem.jpg");
from.renameTo(to);
票数 146
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4178168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档