我们,
我有一段代码可以搜索一些类似的.ser文件并将它们加载到列表中
文件是(rulesIncr1.ser,rulesIncr2.ser,rulesIncr3.ser ......等等)
现在加载我编写了以下逻辑的所有文件
String defaultfilename = "rulesincr";
int i=1;
String incrFile;
//THE FOLLOWING CODE WILL CHECK FOR ANY NU8MBER OF INCR RULES FILE IN THE LOCATION AND ADD THEM TO A RULE MODEL LIST
do
{
String tempincr = new Integer(i).toString();
incrFile = defaultfilename.concat(tempincr).concat(".ser");
FileInputStream fis= new FileInputStream( filePath.concat(incrFile));
ObjectInputStream inStreamIncr = new ObjectInputStream(fis);
myRulesIncr = (List<RuleModel>)inStreamIncr.readObject();
i++;
}
while(new File(filePath.concat(incrFile)).isFile());
现在我遇到的问题是每次myRulesIncr都会刷新,最后只加载最后一个文件。我需要拥有所有加载的文件。请指教
由于
答案 0 :(得分:2)
该行
myRulesIncr = (List<RuleModel>)inStreamIncr.readObject();
循环中的将始终覆盖myRulesIncr
变量指向的List。如果您要将所有RuleModel
个实例添加到myRulesIncr
,您应该有类似
List<RuleModel> myRulesIncr = new ArrayList<RuleModel>();
while{
//your while loop without the
//myRulesIncr = (List<RuleModel>)inStreamIncr.readObject(); line
myRules.addAll( (List<RuleModel>)inStreamIncr.readObject() );
}
答案 1 :(得分:0)
我不熟悉标准列表对象但问题似乎在这里:
myRulesIncr = (List<RuleModel>)inStreamIncr.readObject();
您似乎每次都会创建一个新列表,即使不是,我相信您需要增加到下一个节点,即myRulesIncr = myRulesIncr.next()