-1

我想将 pdf 文件从应用程序路径(/data/data/package name)复制到 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();
        }
    }

它不工作。请帮助。

4

1 回答 1

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());         
        }
    }
于 2012-06-29T15:22:01.783 回答