如何将文件从 .jar 复制到本地 FS
How can i copy Files from .jar into local FS
我想在运行时将我的文件从 .jar 保存到本地 FS。如果我用 netbeans 启动它,我可以保护文件,但如果我启动我的 dist .jar,我找不到文件...
我尝试使用 MyClass.clas.getResource("src/scripteInklErklaerung/Mustertest_24STUNDENBELASTUNG.tts").toExternalForm ...
谁能告诉我正确的方法?
我有一个包含 5 个复选框的 .fxml 文件,如果您 select 多个复选框,您可以单击安全按钮将文件安全保存到本地 FS:
@FXML Button btn_infoAusf;
@FXML CheckBox cb_ausfuehrlich;
@FXML CheckBox cb_eingang;
@FXML CheckBox cb_ausgang;
@FXML CheckBox cb_schnell;
@FXML CheckBox cb_24;
@FXML Button btn_saveToStick;
File pathAusf = new File("/scripteInklErklaerung/Mustertest_AUSFUEHRLICH.tts");
File pathEing = new File("src/scripteInklErklaerung/Mustertest_EINGANG.tts");
File pathAusg = new File("src/scripteInklErklaerung/Mustertest_AUSGANG.tts");
File pathSchnell = new File("src/scripteInklErklaerung/Mustertest_SCHNELL.tts");
File path24 = new File("src/scripteInklErklaerung/Mustertest_24STUNDENBELASTUNG.tts");
ObservableList<CheckBox> cb = FXCollections.observableArrayList();
ObservableList<File> scripte = FXCollections.observableArrayList();
Stage primaryStage = new Stage();
public void getPrimaryStage(Stage stage){
this.primaryStage=stage;
}
public void createPath(){
if(cb_ausfuehrlich.isSelected()){
scripte.add(pathAusf);
}
if(cb_eingang.isSelected()){
scripte.add(pathEing);
}
if(cb_ausgang.isSelected()){
scripte.add(pathAusg);
}
if(cb_schnell.isSelected()){
scripte.add(pathSchnell);
}
if(cb_24.isSelected()){
scripte.add(path24);
}
}
public void saveTestsToStick(ActionEvent event){
try{
createPath();
System.getProperty("java.class.path");
DirectoryChooser fileSaveDialog = new DirectoryChooser();
fileSaveDialog.setTitle("Vorgefertigte Dauertest speichern Pfad: toolhouseUSB-Stick/testlx/DAUERTEST");
Stage stage = (Stage) btn_saveToStick.getScene().getWindow();
stage.close();
File saveFile = fileSaveDialog.showDialog(primaryStage);
for(int i=0;i<scripte.size();i++){
File realPath = new File(saveFile+"\"+scripte.get(i).getName());
Files.copy(scripte.get(i).toPath(), realPath.toPath(), REPLACE_EXISTING);
System.out.println(scripte.get(i));
}
}catch(NullPointerException|IOException ex){System.out.println(ex);}
}
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
btn_saveToStick.setOnAction(this::saveTestsToStick);
cb.add(cb_ausfuehrlich);
}
}
我认为如果您引用 jar 中的文件,您将需要一个绝对路径,所以我会尝试使用:
MyClass.clas.getResource("/src/scripteInklErklaerung/Mustertest_24STUNDENBELASTUNG.tts")
注意路径开头的 /
。
如果这没有帮助,可能是 Files
无法使用来自 Jars 内部的路径。在这种情况下,从您的 Resource 获取一个 InputStream
,为您的目标文件打开一个 FileOutputStream
,然后从 InputStream 读取内容并将其写入您的 OutputStream 以手动将文件复制出您的 Jar:
public static void copyStream(final InputStream fromStream, final OutputStream toStream) throws IOException {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fromStream.read(buffer)) != -1) {
toStream.write(buffer, 0, bytesRead);
}
}
我想在运行时将我的文件从 .jar 保存到本地 FS。如果我用 netbeans 启动它,我可以保护文件,但如果我启动我的 dist .jar,我找不到文件...
我尝试使用 MyClass.clas.getResource("src/scripteInklErklaerung/Mustertest_24STUNDENBELASTUNG.tts").toExternalForm ...
谁能告诉我正确的方法?
我有一个包含 5 个复选框的 .fxml 文件,如果您 select 多个复选框,您可以单击安全按钮将文件安全保存到本地 FS:
@FXML Button btn_infoAusf;
@FXML CheckBox cb_ausfuehrlich;
@FXML CheckBox cb_eingang;
@FXML CheckBox cb_ausgang;
@FXML CheckBox cb_schnell;
@FXML CheckBox cb_24;
@FXML Button btn_saveToStick;
File pathAusf = new File("/scripteInklErklaerung/Mustertest_AUSFUEHRLICH.tts");
File pathEing = new File("src/scripteInklErklaerung/Mustertest_EINGANG.tts");
File pathAusg = new File("src/scripteInklErklaerung/Mustertest_AUSGANG.tts");
File pathSchnell = new File("src/scripteInklErklaerung/Mustertest_SCHNELL.tts");
File path24 = new File("src/scripteInklErklaerung/Mustertest_24STUNDENBELASTUNG.tts");
ObservableList<CheckBox> cb = FXCollections.observableArrayList();
ObservableList<File> scripte = FXCollections.observableArrayList();
Stage primaryStage = new Stage();
public void getPrimaryStage(Stage stage){
this.primaryStage=stage;
}
public void createPath(){
if(cb_ausfuehrlich.isSelected()){
scripte.add(pathAusf);
}
if(cb_eingang.isSelected()){
scripte.add(pathEing);
}
if(cb_ausgang.isSelected()){
scripte.add(pathAusg);
}
if(cb_schnell.isSelected()){
scripte.add(pathSchnell);
}
if(cb_24.isSelected()){
scripte.add(path24);
}
}
public void saveTestsToStick(ActionEvent event){
try{
createPath();
System.getProperty("java.class.path");
DirectoryChooser fileSaveDialog = new DirectoryChooser();
fileSaveDialog.setTitle("Vorgefertigte Dauertest speichern Pfad: toolhouseUSB-Stick/testlx/DAUERTEST");
Stage stage = (Stage) btn_saveToStick.getScene().getWindow();
stage.close();
File saveFile = fileSaveDialog.showDialog(primaryStage);
for(int i=0;i<scripte.size();i++){
File realPath = new File(saveFile+"\"+scripte.get(i).getName());
Files.copy(scripte.get(i).toPath(), realPath.toPath(), REPLACE_EXISTING);
System.out.println(scripte.get(i));
}
}catch(NullPointerException|IOException ex){System.out.println(ex);}
}
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
btn_saveToStick.setOnAction(this::saveTestsToStick);
cb.add(cb_ausfuehrlich);
}
}
我认为如果您引用 jar 中的文件,您将需要一个绝对路径,所以我会尝试使用:
MyClass.clas.getResource("/src/scripteInklErklaerung/Mustertest_24STUNDENBELASTUNG.tts")
注意路径开头的 /
。
如果这没有帮助,可能是 Files
无法使用来自 Jars 内部的路径。在这种情况下,从您的 Resource 获取一个 InputStream
,为您的目标文件打开一个 FileOutputStream
,然后从 InputStream 读取内容并将其写入您的 OutputStream 以手动将文件复制出您的 Jar:
public static void copyStream(final InputStream fromStream, final OutputStream toStream) throws IOException {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fromStream.read(buffer)) != -1) {
toStream.write(buffer, 0, bytesRead);
}
}