android - 如何从 txt 文件中获取某些值?

标签 android file

我是 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/

相关文章:

android - 未知的 Chrome 错误 : 0

java - 获取执行 Java 代码的位置

Ruby 的 File.open 和 f.close 的必要性

java - zip 文件的内容写入不正确

javascript - 如何在 Codeigniter 中添加和使用外部 js 文件?

android - 安全地限制对 FileProvider 的访问

android - 使用 nodejs 相应地重定向到 PlayStore 或 AppStore 的公共(public)链接

java - SharedPreference不保存以前的数据

android - PWA 未显示在 Android 应用程序抽屉中

file - 在一个 jsp 页面中,单击按钮时,在 Bootstrap 中将第二个 jsp 页面加载为模态