【发布时间】:2012-08-07 10:01:07
【问题描述】:
这是我的应用目录
----dist
+-- lib //library folder
+-- backup //folder with my database backup filename = database_file.sqlite
|__ app.jar
|__ database_file.sqlite
当我对按钮执行操作时,我想关闭数据库连接,然后将 database_file.sqlite 从 backup 文件夹复制(覆盖)到根文件夹(dist) 其中 database_file.sqlite 已加载到我的应用程序中。不久,我想在单击按钮时重新加载备份/原始数据库。 注意:我使用默认包 现在我有这段代码(见下文),但我不知道如何使它工作。
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);
}
}
}
【问题讨论】:
-
您遇到了什么问题?
-
java.io.FileNotFoundException: database_file.sqlite(The system cannot find the file specified)我还将OutputStream修改为null。问题在于路径(相对路径)。我不想通过绝对路径让它变得困难,它可以工作,我想要相对于不同操作系统、系统的迁移。感谢您的宝贵时间。 -
您是否真的需要重新初始化文件,因为 inStream 已经正确分配了备份流?
标签: java file inputstream relative-path