Java将配置类保存到可编辑文件中

Java将配置类保存到可编辑文件中,java,serialization,config,Java,Serialization,Config,您好,我想保存一个Java类,该类包含我的应用程序的一些配置 例如: 我希望这个文件可以由用户编辑,乍一看JSONSerializer对于一个简单的例子来说是可以的。 但是我想要更多。。。例如,将来我想在我的类中添加/删除字段。。。它还能用吗? (如果添加了一个字段,我希望在加载包含旧类的文件时使用默认值…) 我不知道我是否清楚。。。 但如果我可以加上 public String c= "hello"; 然后加载文件(不包含字段c的值)。 谢谢大家! 编辑: 这就是我正在尝试的: public

您好,我想保存一个Java类,该类包含我的应用程序的一些配置 例如:

我希望这个文件可以由用户编辑,乍一看JSONSerializer对于一个简单的例子来说是可以的。 但是我想要更多。。。例如,将来我想在我的类中添加/删除字段。。。它还能用吗? (如果添加了一个字段,我希望在加载包含旧类的文件时使用默认值…)

我不知道我是否清楚。。。 但如果我可以加上

public String c= "hello";
然后加载文件(不包含字段c的值)。 谢谢大家!

编辑: 这就是我正在尝试的:

public class PropertiesSerializer<T> {
    private final Class clDef;
    private T obj;

    public T get(){ return obj; }

    public PropertiesSerializer(Class cl){ //uhm...
        if ( cl == null ) throw new NullPointerException();
        clDef= cl;
    }

    public void init() throws InstantiationException {
        Constructor[] ctors = clDef.getDeclaredConstructors();
        Constructor ctor = null;
        for (int i = 0; i < ctors.length; i++) {
            ctor = ctors[i];
            if (ctor.getGenericParameterTypes().length == 0)
                break;
        }

        ctor.setAccessible(true);
        try {
            obj = (T) ctor.newInstance();
        } catch (IllegalAccessException e) {
            throw new InstantiationException(e.getMessage());
        } catch (InvocationTargetException e) {
            throw new InstantiationException(e.getMessage());
        }
    }

    public void load(String filename) throws IOException, InstantiationException {
        File fl= new File(filename);
        Properties props= new Properties();
        try (FileReader reader= new FileReader(fl)) {
            props.load(reader);
        }

        init();
        for(Field fd: clDef.getFields()){
            try {
                Object valobj= props.getProperty( fd.getName() );
                obj.getClass().getField( fd.getName() ).set( obj, fd.getType().cast(valobj) ); //uhm...
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
    }

    public void save(String filename) throws InvalidStateException, IllegalAccessException, IOException {
        if( obj == null ) throw new InvalidStateException("init wasn't called!");

        File fl= new File(filename);
        Properties prop= new Properties();
        for( Field fd: clDef.getFields() ){
            prop.setProperty( fd.getName(), String.valueOf(fd.get(obj)) ); //uhm...
        }

        try( FileWriter writer = new FileWriter(fl) ) {
            prop.store(writer, "PropertiesSerializer");
        }
    }
}
但我无法加载:(失败:

线程“main”java.lang.ClassCastException中出现异常:无法强制转换 java.lang.String转换为java.lang.Class.cast(Class.java:3369)处的布尔值 在 net.sf.aldrigo.propertiesSerializer.propertiesSerializer.load(propertiesSerializer.java:55) 在 net.sf.aldrigo.propertiesSerializer.MainTest.main(MainTest.java:17) 位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 位于java.lang.reflect.Method.invoke(Method.java:497) com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


通过反射查看或使用并滚动您自己的。

到目前为止,您尝试了什么?查看并
storeToXML()
。你可以做你想做的事,但是每次都可以在运行时编译类,这不是一个很好的主意,搜索表单在运行时编译java类我不想重新编译!我尝试过实现一个自定义序列化程序,但我不擅长反射…
使用java.util.Properties并通过反射滚动你自己的属性
我尝试过…阅读我的代码…但我无法找到工作,我不喜欢每次都强制转换…或为每个类编写它…我的解决方案希望是通用的…不过我会看看SimpleXmli如果你深入思考,一旦你编写了它,你就不必“为每个类”编写它。如果它使用泛型。不过我还是衷心推荐SimpleXML。它为您提供了与C#中提供的XML序列化相同的便利。巨大的胜利。唯一的问题是,如果您稍后向模型添加额外字段,那么您需要添加适当的标记来标记它是否应该序列化。但是,正如名称所示,这是“简单的”:
public class PropertiesSerializer<T> {
    private final Class clDef;
    private T obj;

    public T get(){ return obj; }

    public PropertiesSerializer(Class cl){ //uhm...
        if ( cl == null ) throw new NullPointerException();
        clDef= cl;
    }

    public void init() throws InstantiationException {
        Constructor[] ctors = clDef.getDeclaredConstructors();
        Constructor ctor = null;
        for (int i = 0; i < ctors.length; i++) {
            ctor = ctors[i];
            if (ctor.getGenericParameterTypes().length == 0)
                break;
        }

        ctor.setAccessible(true);
        try {
            obj = (T) ctor.newInstance();
        } catch (IllegalAccessException e) {
            throw new InstantiationException(e.getMessage());
        } catch (InvocationTargetException e) {
            throw new InstantiationException(e.getMessage());
        }
    }

    public void load(String filename) throws IOException, InstantiationException {
        File fl= new File(filename);
        Properties props= new Properties();
        try (FileReader reader= new FileReader(fl)) {
            props.load(reader);
        }

        init();
        for(Field fd: clDef.getFields()){
            try {
                Object valobj= props.getProperty( fd.getName() );
                obj.getClass().getField( fd.getName() ).set( obj, fd.getType().cast(valobj) ); //uhm...
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
    }

    public void save(String filename) throws InvalidStateException, IllegalAccessException, IOException {
        if( obj == null ) throw new InvalidStateException("init wasn't called!");

        File fl= new File(filename);
        Properties prop= new Properties();
        for( Field fd: clDef.getFields() ){
            prop.setProperty( fd.getName(), String.valueOf(fd.get(obj)) ); //uhm...
        }

        try( FileWriter writer = new FileWriter(fl) ) {
            prop.store(writer, "PropertiesSerializer");
        }
    }
}
public class TestClass implements Serializable {
    public boolean a= true;
    //public int i= 1;
}