下面這兩種方法,我想讀內src/com/WindThunderStudio/resources
文件在我的應用程序讀取/加載用戶輸入:JAR文件的閱讀,「文件:」的一部分添加時執行`jar`但不能作爲項目
private void saveInputToFile() {
String[][] input = new String[7][4];
//save data to array
String uri = Constants.INPUT_DATA_PATH;
URL url = getClass().getResource(uri);
File f = null;
System.out.println("Writing data...");
if (url != null) {
System.out.println("Input data path: " + url.getFile());
f = new File(url.getFile());
} else {
System.out.println("URL is null. Searching for parent folder. ");
String uriNoQuery = uri.substring(0, uri.lastIndexOf("/"));
System.out.println("uriNoQuery is: " + uriNoQuery);
url = getClass().getResource(uriNoQuery);
if (url != null) {
f = new File(url.getFile() + File.separator + uri.substring(uri.lastIndexOf("/") + 1, uri.length()));
System.out.println("Supposed file absolute path: " + f.getAbsolutePath());
System.out.println("Supposed file path: " + f.getPath());
} else {
System.out.println("Cannot construct the URL with " + uriNoQuery);
}
}
try {
if (!f.exists()) {
if (f.createNewFile()){
System.out.println("Succeeded in creating file. ");
} else {
System.out.println("Creating file failed. ");
}
}
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(input);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void recoverInputFromFile() {
try {
System.out.println("Reading data...");
String uri = Constants.INPUT_DATA_PATH;
URL url = getClass().getResource(uri);
if (url != null){ //if the URL is found. If not found, is null.
System.out.println("Input data path: " + url.getFile());
File f = new File(url.getFile());
if (f.exists()){
System.out.println("File exists. Path: " + f.getAbsolutePath());
ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
String[][] input = new String[7][4];
input = (String[][])in.readObject();
//recover data from `input`
in.close();
} else {
System.out.println("File doesn't exist. " + url.getFile());
}
} else {
System.out.println("URI is null. " + uri);
}
} catch (Exception e) {
e.printStackTrace();
}
}
,其中
Constants.INPUT_DATA_PATH = "/com/WindThunderStudio/resources/inputData.txt";
當應用程序運行時,它會搜索是否有一個URL
existante與給定的路徑。如果沒有,什麼都不做。退出時,它檢查是否使用給定的路徑,可以構造URL
的對象,如果不是,則更改父文件夾並在該文件夾中創建文件,並將數據寫入該文件。
當作爲在Eclipse Java項目運行,它是所有罰款。第一次:
Reading data...
URI is null. /com/WindThunderStudio/resources/inputData.txt
Writing data...
URL is null. Searching for parent folder.
uriNoQuery is: /com/WindThunderStudio/resources
Supposed file absolute path: Y:\CtrlRecau_WinSuite_DYL\desarrollo\workspace_NEW\CalSaldoFichaje\bin\com\WindThunderStudio\resources\inputData.txt
Supposed file path: Y:\CtrlRecau_WinSuite_DYL\desarrollo\workspace_NEW\CalSaldoFichaje\bin\com\WindThunderStudio\resources\inputData.txt
Succeeded in creating file.
並保存數據。第二次運行,數據被恢復到表中,我得到了在控制檯這些行:
Reading data...
Input data path: /Y:/CtrlRecau_WinSuite_DYL/desarrollo/workspace_NEW/CalSaldoFichaje/bin/com/WindThunderStudio/resources/inputData.txt
File exists. Path: Y:\CtrlRecau_WinSuite_DYL\desarrollo\workspace_NEW\CalSaldoFichaje\bin\com\WindThunderStudio\resources\inputData.txt
Writing data...
Input data path: /Y:/CtrlRecau_WinSuite_DYL/desarrollo/workspace_NEW/CalSaldoFichaje/bin/com/WindThunderStudio/resources/inputData.txt
但是,如果我打包爲一個可運行的jar(包裝時,我選擇了Extract library from jar
),我得到了這些錯誤:
第一次運行:
d:\desarrollo>java -jar Cal6.jar
Reading data...
URI is null. /com/WindThunderStudio/resources/inputData.txt
Writing data...
URL is null. Searching for parent folder.
uriNoQuery is: /com/WindThunderStudio/resources
Supposed file absolute path: d:\desarrollo\file:\D:\desarrollo\Cal6.jar!\com\WindThunderStudio\resources\inputData.txt
Supposed file path: file:\D:\desarrollo\Cal6.jar!\com\WindThunderStudio\resources\inputData.txt
java.io.IOException: El nombre de archivo, el nombre de directorio o la sintaxis de la etiqueta del volumen no son correctos
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1006)
at com.WindThunderStudio.UI.MainFrame$4.saveInputToFile(MainFrame.java:777)
at com.WindThunderStudio.UI.MainFrame$4.windowClosed(MainFrame.java:711)
at java.awt.Window.processWindowEvent(Window.java:2054)
at javax.swing.JFrame.processWindowEvent(JFrame.java:296)
at java.awt.Window.processEvent(Window.java:2009)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
異常西班牙的原因是指文件名,文件夾名或路徑的語法是錯誤的。
第二次運行是一樣的,因爲以前的運行沒有寫數據。所以沒有任何東西存儲都沒有恢
我注意到file:\
部分,當我執行jar
文件,我相信這是問題所在。我試圖擺脫恆定路徑中領先的/
,但它不好。
如果有人能幫助我,這將是如此漂亮的你。
該項目在GitHub上可以在這裏找到:
https://github.com/WesleyBlancoYuan/SWING.git
,或者
[email protected]:WesleyBlancoYuan/SWING.git