package com.example.wenzi; import java.io.BufferedReader; import java.io.InputStreamReader; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { initDate(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void initDate() throws Exception{ // 循环取得answer_text下的所有文本文件内容 String[] allTxtFile = this.getAssets().list("answer_text"); for (int i = 0; i < allTxtFile.length; i++) { //加入GBK防止出现乱码, BufferedReader reader = new BufferedReader(new InputStreamReader(this .getAssets().open("answer_text/" + allTxtFile[i]), "GBK")); String line = null; int index = 0; String answer = null; String type = null; while ((line = reader.readLine()) != null) { index++; switch (index) { //如果是第一行就是答案 case 1: answer = line; break; //如果是第二行就是类型 case 2: type = line; break; default: break; } } System.out.println(answer); System.out.println(type); System.out.println("----------------------------------------------"); } } }
在Logcat中看到一下结果,可成功输出assets下answer文件夹中文本的信息