如何从文件中提取文本并将其添加到ArrayList中?

时间:2019-07-13 21:22:00

标签: android file kotlin automation

我正在创建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。

2 个答案:

答案 0 :(得分:0)

下面是如何在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)}

}

答案 1 :(得分:0)

我的建议是将文本文件的内容转换为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保存在资产文件夹中名为“ 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文件,并将其映射到Question对象,该对象具有所有QuestionModel列表的定义。

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