我是 Android 的菜鸟,我需要一些帮助。我有一个这样的 .txt 文件:
#nombre iron will#
#fuerza 6#
#const 6#
#habilidad 6#
#intelig 6#
#vitalidad 42#
#aguante 72#
我想要做的是获取 .txt 文件中的每一项并将其存储在一个变量中。例如,在这种情况下,我想要一个名为“nombre”的字符串变量,其值为“Will”,一个名为“fuerza”的 int 变量,其值为“6”,等等。
我怎样才能得到这个?提前致谢,抱歉我的英语不好。
到目前为止我的代码:
File tarjeta = Environment.getExternalStorageDirectory();
File carpeta = new File(tarjeta.getAbsolutePath() + "/Rol/PJs/");
File archivo = new File(carpeta.getAbsolutePath(), archivoabierto); //String archivoabierto = "name.txt"
StringBuilder text = new StringBuilder();
try{
//This is where I get lost, I don't know how to proceed
BufferedReader br = new BufferedReader(new FileReader(archivo));
String line;
while ((line = br.readLine()) != null){
text.append(line);
text.append('\n');
}
br.close();
} catch (IOException e){
Toast.makeText(Personajes.this, "No se pudo cargar "+archivo.getPath(), Toast.LENGTH_SHORT).show();
}
编辑: 我现在使用的是 json 格式,它可以工作。谢谢大家!
最佳答案
你也可以使用
String[] parts = text.split(" ");
String key = parts[0];
String value = parts[1];
但我建议使用 json -格式。
关于android - 如何从 txt 文件中获取某些值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32283678/