Java Code Examples for android.os.FileUtils#copyFileOrThrow()

The following examples show how to use android.os.FileUtils#copyFileOrThrow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Try our best to migrate all files from source to target that match
 * requested prefix.
 *
 * @return the number of files moved, or -1 if there was trouble.
 */
private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
    final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith(prefix);
        }
    });

    int res = 0;
    for (File sourceFile : sourceFiles) {
        final File targetFile = new File(targetDir, sourceFile.getName());
        Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
        try {
            FileUtils.copyFileOrThrow(sourceFile, targetFile);
            FileUtils.copyPermissions(sourceFile, targetFile);
            if (!sourceFile.delete()) {
                throw new IOException("Failed to clean up " + sourceFile);
            }
            if (res != -1) {
                res++;
            }
        } catch (IOException e) {
            Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
            res = -1;
        }
    }
    return res;
}
 
Example 2
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Try our best to migrate all files from source to target that match
 * requested prefix.
 *
 * @return the number of files moved, or -1 if there was trouble.
 */
private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
    final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith(prefix);
        }
    });

    int res = 0;
    for (File sourceFile : sourceFiles) {
        final File targetFile = new File(targetDir, sourceFile.getName());
        Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
        try {
            FileUtils.copyFileOrThrow(sourceFile, targetFile);
            FileUtils.copyPermissions(sourceFile, targetFile);
            if (!sourceFile.delete()) {
                throw new IOException("Failed to clean up " + sourceFile);
            }
            if (res != -1) {
                res++;
            }
        } catch (IOException e) {
            Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
            res = -1;
        }
    }
    return res;
}
 
Example 3
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Try our best to migrate all files from source to target that match
 * requested prefix.
 *
 * @return the number of files moved, or -1 if there was trouble.
 */
private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
    final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith(prefix);
        }
    });

    int res = 0;
    for (File sourceFile : sourceFiles) {
        final File targetFile = new File(targetDir, sourceFile.getName());
        Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
        try {
            FileUtils.copyFileOrThrow(sourceFile, targetFile);
            FileUtils.copyPermissions(sourceFile, targetFile);
            if (!sourceFile.delete()) {
                throw new IOException("Failed to clean up " + sourceFile);
            }
            if (res != -1) {
                res++;
            }
        } catch (IOException e) {
            Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
            res = -1;
        }
    }
    return res;
}
 
Example 4
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Try our best to migrate all files from source to target that match
 * requested prefix.
 *
 * @return the number of files moved, or -1 if there was trouble.
 */
private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
    final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith(prefix);
        }
    });

    int res = 0;
    for (File sourceFile : sourceFiles) {
        final File targetFile = new File(targetDir, sourceFile.getName());
        Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
        try {
            FileUtils.copyFileOrThrow(sourceFile, targetFile);
            FileUtils.copyPermissions(sourceFile, targetFile);
            if (!sourceFile.delete()) {
                throw new IOException("Failed to clean up " + sourceFile);
            }
            if (res != -1) {
                res++;
            }
        } catch (IOException e) {
            Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
            res = -1;
        }
    }
    return res;
}
 
Example 5
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Try our best to migrate all files from source to target that match
 * requested prefix.
 *
 * @return the number of files moved, or -1 if there was trouble.
 */
private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
    final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith(prefix);
        }
    });

    int res = 0;
    for (File sourceFile : sourceFiles) {
        final File targetFile = new File(targetDir, sourceFile.getName());
        Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
        try {
            FileUtils.copyFileOrThrow(sourceFile, targetFile);
            FileUtils.copyPermissions(sourceFile, targetFile);
            if (!sourceFile.delete()) {
                throw new IOException("Failed to clean up " + sourceFile);
            }
            if (res != -1) {
                res++;
            }
        } catch (IOException e) {
            Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
            res = -1;
        }
    }
    return res;
}
 
Example 6
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Try our best to migrate all files from source to target that match
 * requested prefix.
 *
 * @return the number of files moved, or -1 if there was trouble.
 */
private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
    final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith(prefix);
        }
    });

    int res = 0;
    for (File sourceFile : sourceFiles) {
        final File targetFile = new File(targetDir, sourceFile.getName());
        Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
        try {
            FileUtils.copyFileOrThrow(sourceFile, targetFile);
            FileUtils.copyPermissions(sourceFile, targetFile);
            if (!sourceFile.delete()) {
                throw new IOException("Failed to clean up " + sourceFile);
            }
            if (res != -1) {
                res++;
            }
        } catch (IOException e) {
            Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
            res = -1;
        }
    }
    return res;
}
 
Example 7
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Try our best to migrate all files from source to target that match
 * requested prefix.
 *
 * @return the number of files moved, or -1 if there was trouble.
 */
private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
    final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith(prefix);
        }
    });

    int res = 0;
    for (File sourceFile : sourceFiles) {
        final File targetFile = new File(targetDir, sourceFile.getName());
        Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
        try {
            FileUtils.copyFileOrThrow(sourceFile, targetFile);
            FileUtils.copyPermissions(sourceFile, targetFile);
            if (!sourceFile.delete()) {
                throw new IOException("Failed to clean up " + sourceFile);
            }
            if (res != -1) {
                res++;
            }
        } catch (IOException e) {
            Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
            res = -1;
        }
    }
    return res;
}
 
Example 8
Source File: AccountsDb.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static boolean migratePreNDbToCe(File oldDbFile, File ceDbFile) {
    Slog.i(TAG, "Moving pre-N DB " + oldDbFile + " to CE " + ceDbFile);
    try {
        FileUtils.copyFileOrThrow(oldDbFile, ceDbFile);
    } catch (IOException e) {
        Slog.e(TAG, "Cannot copy file to " + ceDbFile + " from " + oldDbFile, e);
        // Try to remove potentially damaged file if I/O error occurred
        deleteDbFileWarnIfFailed(ceDbFile);
        return false;
    }
    return true;
}
 
Example 9
Source File: ContextImpl.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Try our best to migrate all files from source to target that match
 * requested prefix.
 *
 * @return the number of files moved, or -1 if there was trouble.
 */
private static int moveFiles(File sourceDir, File targetDir, final String prefix) {
    final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith(prefix);
        }
    });

    int res = 0;
    for (File sourceFile : sourceFiles) {
        final File targetFile = new File(targetDir, sourceFile.getName());
        Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
        try {
            FileUtils.copyFileOrThrow(sourceFile, targetFile);
            FileUtils.copyPermissions(sourceFile, targetFile);
            if (!sourceFile.delete()) {
                throw new IOException("Failed to clean up " + sourceFile);
            }
            if (res != -1) {
                res++;
            }
        } catch (IOException e) {
            Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
            res = -1;
        }
    }
    return res;
}