这是我的应用目录
----dist
+-- lib //library folder
+-- backup //folder with my database backup filename = database_file.sqlite
|__ app.jar
|__ database_file.sqlite
当我对按钮执行操作时,我想关闭数据库连接,然后将database_file.sqlite从备份文件夹复制(覆盖)到将database_file.sqlite加载到我的应用程序的根文件夹(dist) 。不久,我想在单击按钮时重新加载备份/原始数据库。注意:我使用默认包现在我有这个代码(见下文),但我不知道如何使它工作。
private void cmd_backupActionPerformed(java.awt.event.ActionEvent evt) {
int p = JOptionPane.showConfirmDialog(null, "Do you really want to reset your data / Backup database?", "Backup", JOptionPane.YES_NO_OPTION);
if (p == 0) {
InputStream inStream = null;
OutputStream outStream = null;
try {
rs.close();
pst.close();
File afile = new File("C:\\Users\\Tzontonel\\Documents\\NetBeansProjects\\RDSS\\dist\\backup\\database_file.sqlite");
File bfile = new File("C:\\Users\\Tzontonel\\Documents\\NetBeansProjects\\RDSS\\dist\\database_file.sqlite");
System.out.println(afile.getCanonicalPath());
inStream = new FileInputStream(afile);
outStream = new FileOutputStream(bfile);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
// delete the original file
// afile.delete();
System.out.println("File is copied successful!");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}