0

如果我在这种组合中使用它,代码会中断:保存一个值,删除一个值,保存一个值。当我第二次保存该值时,它甚至会将已删除的值添加回它。因此,如果我保存配置 1、2、3、4、删除 3 和保存 5,这将保存 3 以及最终值:1、2、3、4、5,即使我删除了 3。

这是我认为重要的代码

    /**
 * This is the save class.
 * This class contains the action for when the user clicks on the save button.
 */
package controller;
/**
 * These are the imports for the class.
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import model.ModelFacade;

public class Save {
    /**
     * These are the fields for the class.
     */
    ModelFacade mf = new ModelFacade();
    Properties prop = new Properties();

    /**
     * This method sets the entered value for the matching property.
     * 
     * @param property
     * @param value
     */
    public void setPropertyValue(String property, String value) {
        try {
            try{
                //set the properties value
                FileInputStream fis = new FileInputStream("config.properties");
                prop.load(fis);
                fis.close();
                prop.setProperty(property, value);
            }
                catch (IOException ex) {
            }

            //save properties to project root folder
            FileOutputStream fos = new FileOutputStream("config.properties");
            prop.store(fos, null);
            fos.close();
            System.out.println("saved " + property + " as " + value);
        } 
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}


    /**
 * This is the delete class.
 * This class contains the action for when the user clicks on the delete button.
 */
package controller;
/**
 * These are the imports of the class.
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import model.ModelFacade;

public class Delete {
    /**
     * These are the fields of the class.
     */
ModelFacade mf = new ModelFacade();
Properties prop = new Properties();

    /**
     * This method deletes the values of the selected config.
     * 
     * @param config
     */
    public void deleteAction(String config) {
        if(mf.checkProperty() == true){
            try { 
                FileInputStream fis = new FileInputStream("config.properties");
                prop.load(fis);
                fis.close();
                @SuppressWarnings("rawtypes")
                Enumeration em = prop.keys();
                int i = 0;
                while(em.hasMoreElements()){
                     Object obj = em.nextElement();
                     String str = (String)obj;
                     //If the property contains the configuration name in its name it will be deleted.
                     if(str.endsWith(config)){
                         i++;
                         System.out.println("Deleted: "+str);
                         prop.remove(str);
                         FileOutputStream fos = new FileOutputStream("config.properties");
                         prop.store(fos, null);
                         fos.close();
                     }
                }
                //The system prints a message of the missing configuration.
                if(i < 1){
                    System.out.println("The configuration could not be found.");
                }
            } 

            catch (IOException ex) {
                ex.printStackTrace();
            }

        }
        //The system prints a message that the property file could not be found.
        else{
            System.out.println("The property file could not be found.");
        }
    }
}

这是正在使用的两种方法。我希望这是足够的信息对您有所帮助。很抱歉我的话太短了。我现在有点累而且时间很短。如果需要,我可以更详细地解释这一点。但我有点希望问题出在这段代码中。

4

0 回答 0