【问题标题】:How to extract text from file and add it into an ArrayList?如何从文件中提取文本并将其添加到 ArrayList 中?
【发布时间】:2019-07-13 21:22:00
【问题描述】:

我正在创建一个 QCM 应用程序,所以我想从像 this one 这样的文本文件中获取问题和答案,然后获取问题和答案,然后将其全部添加到这样的数组中:

var myQstList: Array<String> = arrayOf("")
myQstList = arrayOf(
                    "myQestion1",
                    "myQestion2",
                    "myQestion3",
                    "myQestion4"
                    )

var myAns: Array<String> = arrayOf("")
myAns= arrayOf(
                    "myOption1",
                    "myOption2",
                    "myOption3",
                    "myOption4"
                    )

我想对每个问题都做同样的事情,它的答案请参阅 doc了解我的情况。

注意:我在我的 android 应用上使用 kotlin。

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 我尝试手动完成

标签: android file kotlin automation


【解决方案1】:

我的建议是将文本文件的内容转换为 Json 文档,将 Json 文档直接映射到对象会更容易和更清晰。例如:

{
[
{
"question1":["answer1","answer2","answer3"]
},
{
"question2":["answer1","answer2","answer3"]
}
]
}

好的,让我们一步一步开始实现。

使用以下方法将 Gson 添加到您的项目中:

implementation 'com.google.code.gson:gson:2.8.5' 

这里是 json 将被映射到的示例对象:

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Question {

@SerializedName("questionModel")
@Expose
private List<QuestionModel> questionModel = null;

public List<QuestionModel> getQuestionModel() {
return questionModel;
}

public void setQuestionModel(List<QuestionModel> questionModel) {
this.questionModel = questionModel;
}

}

将其保存在另一个名为 QuestionModel.java 的文件中

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class QuestionModel {

@SerializedName("question")
@Expose
private String question;
@SerializedName("answers")
@Expose
private List<String> answers = null;

public String getQuestion() {
return question;
}

public void setQuestion(String question) {
this.question = question;
}

public List<String> getAnswers() {
return answers;
}

public void setAnswers(List<String> answers) {
this.answers = answers;
}

}

这也是您的问题的示例 Json

  {
  "questionModel":[
    {
"question" : "This is a sample question",
"answers" : ["answer1","answer2","answer3"]
}
  ]

}

您可以将 json 保存在 assets 文件夹中名为“sample.json”的文件中,读取内容并使用以下代码解析为对象:

String jsonSring = null;
try {
InputStream is = getAssets().open("data/sample.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
jsonString = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
}

Gson gson = new Gson();
Question questionObject = gson.fromJson(jsonSring, Question.class);

所以你有了它,包含所有问题的 json 文件已映射到问题对象,该对象具有所有 QuestionModel 列表的定义。

如您所见,每个 QuestionModel 都有一个问题和可能的答案列表。

【讨论】:

  • 我的问题是如何提取问题和选项,然后将其添加到 ArrayList
  • 好吧,如果你同意使用我建议的这种方法,那么我可以编写代码来进行提取。
  • 是的诅咒,如果它有效,我会确认你的答案
【解决方案2】:

下面是一个示例,说明如何在 kotlin 中读取文件的每一行。我的例子只是打印出每一行。将其替换为您想要对它们执行的任何操作。例如将它们添加到数组中。

fun main(args: Array<String>) {
  val inputStream: InputStream = File("yourfile.txt").inputStream()
  val lineList = mutableListOf<String>()

  inputStream.bufferedReader().useLines { lines -> lines.forEach { lineList.add(it)} }

  lineList.forEach{println(">  " + it)}

}

【讨论】:

  • 如果您在上面的文档中看到,您会看到问题后面是一个空行,然后是五行选项...请您编辑您的代码以使其更适用于这种情况
猜你喜欢
  • 2019-04-12
  • 1970-01-01
  • 2016-04-26
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 2016-08-27
  • 2022-01-07
相关资源
最近更新 更多