用另一个覆盖非文本文件

Overwriting a non-text file with another

我不确定如何覆盖非文本文件。如何用我的 default.db 文件覆盖我的 main.db 文件 (sqlite 3)?

private void yesAction() {

    try {

        String text = Files.readString(Paths.get("src\main\database\default.db"));

        System.out.println(text);

        Files.writeString(Paths.get("src\main\database\main.db"), text, Charset.defaultCharset());

    } catch (IOException e) {

        e.printStackTrace();
    }
}

您可以使用此 FileChannel 覆盖 db 文件或任何其他文件。

try {
    File oldDb = new File(old, oldDbPath);
    File newDb = new File(new, newDbPath);

    if (newDb.exists()) {
        FileChannel oldDbChannel = new FileInputStream(oldDb).getChannel();
        FileChannel newDbChannel = new FileOutputStream(newDb).getChannel();
        newDbChannel.transferFrom(oldDbChannel, 0, oldDbChannel.size());
        oldDbChannel.close();
        newDbChannel.close();
    }

} catch (Exception e) {
    e.printStackTrace();
}

您可以在此处阅读有关 FileChannel 的更多信息 https://developer.android.com/reference/java/nio/channels/FileChannel