在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;
}

所屬標簽

無標簽

25选5玩法中奖