我正在嘗試讀取jar文件的內容。下面的代碼我已經寫了這個。我已經將jar文件放在lib目錄下(\ practice \ lib \ abc.jar)。如何從jar中讀取xml文件
final InputStream is = JarReader.class.getResourceAsStream("/abc.jar");
BufferedReader input = new BufferedReader(new InputStreamReader(is));
Scanner scan = new Scanner(input);
while (scan.hasNext()) {
String s = scan.next();
System.out.println(s);
}
scan.close();
每次inputStream都爲空。後下我用我的參考 getResourceAsStream returns null How to read a file from jar in Java?
等等呢?你希望我們爲你寫代碼嗎?你有什麼嘗試?什麼不按預期工作? – stellasia
我試着用下面的代碼final InputStream internalMetadataInputStream = this.getClass()。getClassLoader() \t \t \t \t .getResourceAsStream(DirString); \t \t BufferedReader inp = new BufferedReader(new InputStreamReader(internalMetadataInputStream)); \t \t \t \t Scanner scan = new Scanner(inp); \t \t dl.debug(「scan.hasNext()::」+ scan.hasNext()); (scan.hasNext()){ \t while(scan.hasNext()){ \t String s = scan.next(); \t \t} \t scan.close();但它正在逐行掃描文件內容。 –
請用這段代碼編輯你的問題(這會更容易閱讀)。謝謝。 – stellasia