当原始本地文件发生更改时,如何保护存储在java对象中的文件不受更改的影响?

当原始本地文件发生更改时,如何保护存储在java对象中的文件不受更改的影响?,java,sqlite,javafx,filechooser,Java,Sqlite,Javafx,Filechooser,我有一个java应用程序,它接收文件,将其发送到数据库并将其加载回数据库 由于这是一个分布式应用程序,不同机器中的用户以这种方式进行交互:上载和下载文件 以下是我正在使用的方法: @FXML private void uploadFile(ActionEvent event) throws ClassNotFoundException, IOException, SQLException { FileChooser fileChooser = new FileChoos

我有一个java应用程序,它接收文件,将其发送到数据库并将其加载回数据库

由于这是一个分布式应用程序,不同机器中的用户以这种方式进行交互:上载和下载文件

以下是我正在使用的方法:

@FXML
    private void uploadFile(ActionEvent event) throws ClassNotFoundException, IOException, SQLException {

        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Resource File");
        File file = fileChooser.showOpenDialog(mainStage).getCanonicalFile();
        File file2 = file;

        file2.setWritable(false);
        file2.setReadOnly();        

        FileUtils.copyFileToDirectory(file2, FileUtils.getFile(super.getPath(file2.getName())), true); //This is my attempt to copy the original local file to the application folder and perform changes in this file instead of the original file. Nevertheless, the application is still looking for the local file.

        shl.getCurrentTab().addFile("File-"+file2.getName(), file2);

        super.saveShell();
        super.saveLog(shl.getCurrentUser().getID() + " - " + shl.getCurrentUser().getName() +
                " - " + "UPLOADED "+file2.getName());


        System.out.println("File uploaded as "+file2.toString());
    }

public void saveShell() throws IOException, ClassNotFoundException {

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                new FileOutputStream(getPath("shell.txt")));

        objectOutputStream.writeObject(new Date());
        objectOutputStream.writeBoolean(true);
        objectOutputStream.writeFloat(1.0f);

        objectOutputStream.writeObject(shl);
        objectOutputStream.flush();
        objectOutputStream.close();
        System.out.println("Successfully saved");

        saveShellDB();  
    }

public void saveShellDB() throws ClassNotFoundException, IOException {

        Class.forName(classForName);
        Connection connection = null;

        try
        {
          // create a database connection
          connection = DriverManager.getConnection(connectionPath);
          Statement statement = connection.createStatement();
          statement.setQueryTimeout(30);  // set timeout to 30 sec.

          File file = new File("shell.txt");
          FileInputStream fis = new FileInputStream(file);

          PreparedStatement ps = connection.prepareStatement("INSERT OR REPLACE INTO shell (name,shl) VALUES (?,?)");   
          ps.setString(1, file.getName());
          ps.setBinaryStream(2, fis, (int)file.length());
          ps.executeUpdate();

          ps.close();
          fis.close();

          System.out.println("SQL save done");

        } catch(SQLException e) {

          // if the error message is "out of memory", 
          // it probably means no database file is found
          System.err.println(e.getMessage());
        }

        finally
        {
          try
          {
            if(connection != null)
              connection.close();

          } catch(SQLException e) {
            // connection close failed.
            System.err.println(e);
          }
        }

      }
打开文件时:

@FXML
    public void refreshFileList() throws ClassNotFoundException, SQLException, IOException {  

    //this is the listView object that shows the current files

        fileListView.setItems(FXCollections.observableArrayList(shl.getCurrentTab().fileMapToSet()));
        fileListView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        fileListView.setEditable(true);

        fileListView.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                currentFile = (File) fileListView.getSelectionModel().getSelectedItem();
                System.out.println("clicked on " + fileListView.getSelectionModel().getSelectedItem());

            }
        });
    }

@FXML
private void openFile(ActionEvent event) throws IOException {

    currentFile.setReadOnly();

    Desktop dt = Desktop.getDesktop();
    dt.open(currentFile);

}

private Map<String,File> _files = new HashMap<String,File>();

public Set<File> fileMapToSet() {
    this._fileSet = null;
    this._fileSet = new HashSet<File>();

    for(String key : _files.keySet()) {
        this._fileSet.add(_files.get(key));
    }

    return this._fileSet;
}
@FXML
public void refreshFileList()引发ClassNotFoundException、SQLException、IOException{
//这是显示当前文件的listView对象
setItems(FXCollections.observableArrayList(shl.getCurrentTab().fileMapToSet());
fileListView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
fileListView.setEditable(true);
setOnMouseClicked(新的EventHandler(){
@凌驾
公共无效句柄(MouseeEvent事件){
currentFile=(文件)fileListView.getSelectionModel().getSelectedItem();
System.out.println(“单击“+fileListView.getSelectionModel().getSelectedItem());
}
});
}
@FXML
私有void openFile(ActionEvent事件)引发IOException{
currentFile.setReadOnly();
Desktop dt=Desktop.getDesktop();
打开(当前文件);
}
私有映射_files=newhashmap();
公共集fileMapToSet(){
该文件集为空;
这个。_fileSet=new HashSet();
对于(字符串键:_files.keySet()){
这个._fileSet.add(_files.get(key));
}
返回此文件。\u文件集;
}
我上传了一个文件,但如果之后我更改了本地文件,它会以某种方式在java对象中进行更新。我需要一种方法来保护本地文件不受这种连接的影响,因为只有当用户选择上载新版本时,才应该更新文件


可能是因为de File name通常是文件的路径,比如使用FileUtils的
C:\Users\Test\Book.xls

你说的@trashgood是什么意思?我应该什么时候创建临时文件?临时文件名是唯一的。你说的@trashgood是什么意思?我应该什么时候创建临时文件?临时文件名是唯一的。