[英]How do I select a folder to save a file to in Jython using JFileChooser
[英]How do i save a File from a jfilechooser into the project folder?
我剛剛完成了用Java上傳圖像的操作,現在我想上傳docx,pdf等文件。
我嘗試使用/更改用於上傳圖片的代碼,但沒有任何成功。
private void btnOpenFileActionPerformed(java.awt.event.ActionEvent evt) {
int returnValue = openFileChooser.showOpenDialog(this);
if(returnValue == JFileChooser.APPROVE_OPTION) {
try{
openFileChooser.getSelectedFile();
lblMessage.setText("File Succesfully loaded");
}catch(Exception ex){
lblMessage.setText("Failed to load File");
}
}
else{
lblMessage.setText("No file Choosen");
}
}
這是我選擇文件的方式,並且沒有任何代碼可以在項目文件夾中上載/保存
首先,請確保將所選文件分配給如下所示的變量:
File theSelectedFile = openFileChooser.getSelectedFile();
然后,因為您只需要文件的副本,就可以通過將簡單的文件復制操作添加到下面的行中來使用它:
String placeToSaveFile = "C:/path/to/your/project/";
Files.copy(theSelectedFile.toPath(), new File(placeToSaveFile).toPath(), StandardCopyOption.REPLACE_EXISTING);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.