我想了解资产文件夹中存储的数据文件与存储在res/raw文件夹中的数据文件之间的区别。我的目标是将数据文件存储在应用程序目录结构中(在这种情况下)存储测试成绩,可以访问它们,然后修改它们。这是我的理解,我需要使用ASSET而不是RAW文件。使用存储在资产文件夹中的文本文件
当文本文件存储在RES/RAW文件夹中时,我设法将文本文件中的数据加载到数组中,但现在我无法使用ASSET文件工作。我认为这与使用AssetManager.open一样简单。
最终我的一个问题就是这个。如何读取和写入ASSET文件夹中的文本文件?
这里是我的代码:
public class MainActivity extends AppCompatActivity {
String[][] testScoreList = new String[3][3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load test scores into arraylist
nameArrayListMethod();
}
//This method loads test scores into an array
public void nameArrayListMethod(){
InputStreamReader InputSR = null;
BufferedReader BufferedRdr = null;
String thisLine = null;
AssetManager am = getAssets();
InputSR = new InputStreamReader(am.open("test_scores.txt"));
BufferedRdr = new BufferedReader(InputSR);
try {
// open input stream test_scores for reading purpose.
int i = 0;
while ((thisLine = BufferedRdr.readLine()) != null) {
// System.out.println(thisLine);
String[] parts = thisLine.split(" ");
testScoreList[i][0] = parts[0];
testScoreList[i][1] = parts[1];
i = i +1;
}
} catch (Exception e) {
e.printStackTrace();
}
我得到的(am.open("test_scores.txt"))
线的Unhandled exception: java.io.IOException
eror。
干杯的输入
它为什么会抛出异常?文件test_scores.txt确实存在。你的解决方案的方式,但我不明白为什么。 – Airfix
它不会抛出异常,但它可能是由于无法预料的事情。阅读和写作东西是更冒险的事情之一。与服务器交谈时,情况变得更糟...... – Flummox