How to copy files in java

    public  boolean fileCopy(String from, String to) {
        Log.i("upgrade_copy","from  path  "+from );
        Log.i("upgrade_copy","to  path  "+to );
        boolean result = false;
        int size = 1 * 1024;
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(from);
            out = new FileOutputStream(to);
            byte[] buffer = new byte[size];
            int bytesRead = -1;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            out.flush();
            result = true;
            Log.i("upgrade_copy","copy sucess  " );
            File delFile = new File(from);
            if (delFile.exists()){
                delFile.delete();
                Log.i("upgrade_copy","del from path file  " );
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
            }
        }
        return result;
    }

Guess you like

Origin blog.csdn.net/u010689853/article/details/112917910