如何将文件从应用程序包路径复制到SD卡?

时间:2012-06-29 15:15:02

标签: android

我希望从应用程序路径(/ data / data / package name)复制pdf文件到我准备好的sdcard.for,

try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    }
    finally {
        if(source != null) {
            source.close();
        }
        if(destination != null) {
            destination.close();
        }
    }

它没有用.PLease帮助。

1 个答案:

答案 0 :(得分:1)

以下是复制文件的示例代码

 private static void copyfile(String srFile, String dtFile){
        try{
            File f1 = new File(Source Fine Name);
            File f2 = new File(Destination File Name);
            InputStream in = new FileInputStream(f1);

//                  If you want to append the file.
//          OutputStream out = new FileOutputStream(f2,true);

            //For Overwrite the file.
            OutputStream out = new FileOutputStream(f2);

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0){
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            System.out.println("File copied.");
        }
        catch(FileNotFoundException ex){
            System.out.println(ex.getMessage());

        }
        catch(IOException e){
            System.out.println(e.getMessage());         
        }
    }