【发布时间】:2017-09-25 15:06:16
【问题描述】:
让自己为最简单的事情发疯。我有一个名为 config.txt 的 JSON 文件。该文件如下所示。 { “UsePipesInGuestData”:真 }
我想要做的就是得到一个二维数组,这样: 数组 [0] = UsePipesInGuestData 和 数组[1] = 真
我已经尝试了 4 个小时,各种尝试,我最近的如下所示: 私人无效getConfig(){ //从config.txt中读取配置信息的函数
FileInputStream is;
BufferedReader reader;
try {
final File configFile = new File(Environment.getExternalStorageDirectory().getPath() + "/guestlink/config.txt");
if (configFile.exists()) {
is = new FileInputStream(configFile);
reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
if(line!= null) {
line = line.replace("\"", ""); //Strip out Quotes
line = line.replace(" ", ""); //Strip out Spaces
if ((!line.equals("{")) || (!line.equals("}"))) {
} else {
String[] configValue = line.split(":");
switch (configValue[0]) {
case "UsePipesInGuestData":
if (configValue[1].equals("true")) {
sharedPreferences.edit().putString("UsePipes", "true").apply();
} else {
sharedPreferences.edit().putString("UsePipes", "false").apply();
}
break;
}
}
}
}
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
我似乎无法忽略其中包含 { 和 } 的行。
显然必须有更简单的方法。 JAVA 似乎只需要大量的代码来做最简单的事情。非常感谢任何帮助。
【问题讨论】:
-
你可以使用Gson stackoverflow.com/questions/29965764/…