Java 在jar中读取多个文件
我想读取jar文件中的多个文件。但它不起作用。它只读取一个文件并停止。我真的不明白Java 在jar中读取多个文件,java,jar,Java,Jar,我想读取jar文件中的多个文件。但它不起作用。它只读取一个文件并停止。我真的不明白Inputstream和Bufferedreader是如何工作的。这部分代码是从另一篇文章中借来的 for (int i = 0; i < numList; i++) { // numList is number of file I want to read int fileNum = i + 1; String fileaddress = "List" + fileNum + ".txt
Inputstream
和Bufferedreader
是如何工作的。这部分代码是从另一篇文章中借来的
for (int i = 0; i < numList; i++) { // numList is number of file I want to read
int fileNum = i + 1;
String fileaddress = "List" + fileNum + ".txt";
//All the files have name like "List1.txt", "List2.txt", etc
InputStream configStream = getClass().getResourceAsStream(fileaddress);
BufferedReader wordReader = new BufferedReader(new InputStreamReader(configStream, "UTF-8"));
//the following is to store values in the line in an String array
//add the array to an ArrayList called masterList
String[] line;
while((line = wordReader.readLine().split(": ")) != null){
masterList.add(line);
}
for(int i=0;i
代码是否引发任何异常,或者只是默默地“失败”另外,除了转到numList
的值是多少之外,还有什么理由不从1开始呢?如果这是1,那么它将在第一次读取后停止。您的代码是否有try/catch/finally?您是否阅读了java io教程?虽然这可能不是问题所在,但我强烈建议关闭BufferedReader
和循环体末尾的InputStream
,方法是将while循环包装在try
块中,并将以下内容放入finally
块:wordReader.close();
和configStream.close()
。