-1

伙计们,

我有一段代码可以搜索一些类似的 .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 并且最后只加载最后一个文件。我需要拥有所有加载的文件。请指教

谢谢

4

2 回答 2

2

线

myRulesIncr = (List<RuleModel>)inStreamIncr.readObject();

在您的循环中将始终覆盖myRulesIncr变量指向的列表。如果你想添加所有这些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() );
}
于 2012-04-05T13:48:13.620 回答
0

我不熟悉标准列表对象,但问题似乎在这里:

myRulesIncr = (List<RuleModel>)inStreamIncr.readObject();

您似乎每次都在制作一个新列表,即使不是,我相信您需要递增到下一个节点,即myRulesIncr = myRulesIncr.next()

于 2012-04-05T13:49:44.460 回答