Как сохранить готовый файл в другом месте?

Есть готовый файл

 File file = new File(getFilesDir(),"temp/test.pdf");

Надо его прочитать и сохранить по другому пути как-то так:

File externalAppDir = new File(Environment.getExternalStorageDirectory() + "/Android/data/" + getPackageName());
    if (!externalAppDir.exists()) { externalAppDir.mkdir(); }
    file = new File(externalAppDir , .......);
    try {
        file.createNewFile();
    } catch (IOException e) { e.printStackTrace(); }

Но как именно не понимаю


Ответы (1 шт):

Автор решения: Andrew

Вот есть например функция для решения вашей проблемы:

public static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.getParentFile().exists())
            destFile.getParentFile().mkdirs();

        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;

        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();
            }
        }
    }

Вот документация и первоисточник.

→ Ссылка