Java 将文件列表与对象列表进行比较以删除文件

Java 将文件列表与对象列表进行比较以删除文件,java,Java,我有一个计划列表,每个计划都有一个PDF格式(“/web/managed/”) 我在删除计划时没有删除文件,所以现在我尝试添加一个函数来删除计划列表中没有ID的所有文件 文件名始终具有id。 示例:6365_Test-LVLD.pdf 对象列表: @Transaction public List<StorePlan> getPlans() { List<StorePlan> list = getCurrentSession().createCriteria(St

我有一个计划列表,每个计划都有一个PDF格式(“/web/managed/”) 我在删除计划时没有删除文件,所以现在我尝试添加一个函数来删除计划列表中没有ID的所有文件

文件名始终具有id。 示例:6365_Test-LVLD.pdf

对象列表:

@Transaction
public List<StorePlan> getPlans() {
     List<StorePlan> list = getCurrentSession().createCriteria(StorePlan.class).list();
     return list;
}
以下是我的清除功能:

protected void getPlanIds() {
    int count = 0;
    for(StorePlan plan : storePlanDao.getPlans()) {
        for (File file : getPDFs()) {
            String  planFileId = file.getName().substring(0, 4);

            if(plan.getId() != Integer.valueOf(planFileId)) {
                file.delete();
                count++;
            }
        }
    }   
}

使用我的代码:它将删除我文件夹中的所有内容。当我想在其他列表中保留仍具有ID的文件时。

如果我理解您的问题,那么这应该可以:

    List<Integer> planIds = Lists.newArrayList();   

    for(StorePlan plan : storePlanDao.getPlans()){
        planIds.add(plan.getId());
    }

    for (File file : getPDFs()) {
        Integer planFileId = Integer.valueOf(file.getName().substring(0, 4))
        if(!ids.contains(planFileId)) {
            file.delete();
            count++;
        }
    }
List planIds=Lists.newArrayList();
对于(StorePlan计划:storePlanDao.getPlans()){
planIds.add(plan.getId());
}
对于(文件:getPDFs()){
整数planFileId=Integer.valueOf(file.getName().substring(0,4))
如果(!ids.contains(planFileId)){
delete();
计数++;
}
}

如果我理解了你的问题,那么这应该行得通:

    List<Integer> planIds = Lists.newArrayList();   

    for(StorePlan plan : storePlanDao.getPlans()){
        planIds.add(plan.getId());
    }

    for (File file : getPDFs()) {
        Integer planFileId = Integer.valueOf(file.getName().substring(0, 4))
        if(!ids.contains(planFileId)) {
            file.delete();
            count++;
        }
    }
List planIds=Lists.newArrayList();
对于(StorePlan计划:storePlanDao.getPlans()){
planIds.add(plan.getId());
}
对于(文件:getPDFs()){
整数planFileId=Integer.valueOf(file.getName().substring(0,4))
如果(!ids.contains(planFileId)){
delete();
计数++;
}
}

我想我看到了问题所在。不要在第二个循环中删除问题,而是将布尔值设置为true并中断循环。在第二个循环之外有一个if语句,如果为true,则删除该文件。因此:

protected void getPlanIds() {
int count = 0;
for(StorePlan plan : storePlanDao.getPlans()) {
    Boolean found = false;
    for (File file : getPDFs()) {
        String  planFileId = file.getName().substring(0, 4);

        if(plan.getId() == Integer.valueOf(planFileId)) {
            found = true;
            break;
        } else {
           count++;
        }
    }
    if (!found) {
        file.delete();
    }
}   
}

我为格式错误道歉。我在手机上,在工作中打发时间。xD

我想我看到了问题所在。不要在第二个循环中删除问题,而是将布尔值设置为true并中断循环。在第二个循环之外有一个if语句,如果为true,则删除该文件。因此:

protected void getPlanIds() {
int count = 0;
for(StorePlan plan : storePlanDao.getPlans()) {
    Boolean found = false;
    for (File file : getPDFs()) {
        String  planFileId = file.getName().substring(0, 4);

        if(plan.getId() == Integer.valueOf(planFileId)) {
            found = true;
            break;
        } else {
           count++;
        }
    }
    if (!found) {
        file.delete();
    }
}   
}
我为格式错误道歉。我在手机上,在工作中打发时间。xD