我想從jar文件運行程序時讀取文本文件。 我來accros,我需要使用InputStream來讀取文件。的代碼片段:從那時起從InputStream對象讀取文件
buffer = new BufferedInputStream(this.getClass().getResourceAsStream((getClass().getClassLoader().getResource("English_names.txt").getPath())));
System.out.println(buffer.read()+" yeas");
在這條線System.out.println(buffer.read()+" yeas");
程序停止,沒有任何反應。一旦輸出buffer
對象的內容,它就不爲空。 可能是什麼問題?
你的第一行似乎有點複雜。哪裏存在「English_names.txt」? – SJuan76
在這個階段它位於項目的目錄中。 – uml
好消息,我已經嘗試過FileInputStream傳入InputStream,現在它工作正常。希望它能在您使用jar文件運行應用程序的同時工作。這是使用InputStream – uml