在Android中使用Java复制文件


复制文件,只用到了File及其相关的Stream类。

public static boolean copyFile(String src,String dest,boolean deleteIfExist){
    File fileObj = new File(dest);
    if(fileObj.exists()){
        if(!deleteIfExist){
            return false;
        }

        fileObj.delete();
    }

    InputStream inputStream = null;
    FileOutputStream fileOutputStream = null;
    try{
        fileObj = new File(dest);
        fileObj.createNewFile();

        inputStream = new FileInputStream(src);
        fileOutputStream = new FileOutputStream(dest);

        byte[] buffer = new byte[1024 * 100];
        int byteread = 0;
        while ( (byteread = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer,0,byteread);
        }
    }catch (Exception ex){
        ex.printStackTrace();
    }finally {
        IOHelper.close(inputStream);
        IOHelper.close(fileOutputStream);
    }

    return true;
}
相关标签

扫一扫

在手机上阅读