APP的参数文件用什么形式保存


#1

大家好:
本人用QT(C++)开发应用,一般用INI文件保存程序的一些参数。
现在学习用Kotlin(jvm), 那么还是用INI的文件格式保存能数吗?还是有别的方法?
(INI 文件的段不能嵌套,有点不方便)


#2

常见的有java原生支持的properties,以及一些通用的
yaml
json
xml
个人比较喜欢yaml


#3

Android的话,SharedPreferences吧


#4

JYaml 和 Kotlin 好象不兼容,以下代码(Main.kt) 运行失败!

data class Person(var name: String, var age: Int)

fun main(args: Array) {

var p1 = Person("neeme", 18)

var file = File(System.getProperty("user.home") + "/ini/config.yaml")
Yaml.dump(p1, file)

}


#5

起码贴出报错日志啊


#6

Exception in thread “main” org.ho.yaml.exception.ObjectCreationException: Error near line 0: Can’t create object of type class neeme.Person using default constructor.
at org.ho.yaml.wrapper.AbstractWrapper.createObject(Unknown Source)
at org.ho.yaml.wrapper.AbstractWrapper.createPrototype(Unknown Source)
at org.ho.yaml.wrapper.DefaultBeanWrapper.keys(Unknown Source)
at org.ho.yaml.YamlEncoder.traverseAndCountMap(Unknown Source)
at org.ho.yaml.YamlEncoder.traverseAndCount(Unknown Source)
at org.ho.yaml.YamlEncoder.writeObject(Unknown Source)
at org.ho.yaml.YamlConfig.dump(Unknown Source)
at org.ho.yaml.Yaml.dump(Unknown Source)
at neeme.MainKt.main(Main.kt:19)
Caused by: org.ho.yaml.exception.ObjectCreationException: Error near line 0: Default constructor for class neeme.Person is not accessible.
… 9 more


#7

Exception in thread “main” org.ho.yaml.exception.ObjectCreationException: Error near line 0: Can’t create object of type class neeme.Person using default constructor.
at org.ho.yaml.wrapper.AbstractWrapper.createObject(Unknown Source)
at org.ho.yaml.wrapper.AbstractWrapper.createPrototype(Unknown Source)
at org.ho.yaml.wrapper.DefaultBeanWrapper.keys(Unknown Source)
at org.ho.yaml.YamlEncoder.traverseAndCountMap(Unknown Source)
at org.ho.yaml.YamlEncoder.traverseAndCount(Unknown Source)
at org.ho.yaml.YamlEncoder.writeObject(Unknown Source)
at org.ho.yaml.YamlConfig.dump(Unknown Source)
at org.ho.yaml.Yaml.dump(Unknown Source)
at neeme.MainKt.main(Main.kt:19)
Caused by: org.ho.yaml.exception.ObjectCreationException: Error near line 0: Default constructor for class neeme.Person is not accessible.
… 9 more

就这样啊


#8

给你的数据类加noarg插件试试。或者定义无参构造器


京ICP备16022265号-2 Kotlin China 2017 - 2018