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

时间:2015-09-03 10:03:52

标签: java sqlite javafx filechooser

我有一个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;
}

我上传了一个文件,但是如果之后我更改了我的本地文件,它在某种程度上是在java对象中更新的。我需要一种方法来保护本地文件免受这种连接,因为只有在用户选择上传新版本时才应更新文件。

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

0 个答案:

没有答案