2016-06-22 122 views
1

我想了解资产文件夹中存储的数据文件与存储在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。

干杯的输入

回答

1

AssetManger#打开(字符串)将抛出异常,你需要处理它。

public final InputStream open(String fileName) throws IOException { 
    return open(fileName, ACCESS_STREAMING); 
} 

因此,你需要:

try { 
     InputSR = new InputStreamReader(am.open("test_scores.txt")); 
     BufferedRdr = new BufferedReader(InputSR); 
     // 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(); 

    } 
+0

它为什么会抛出异常?文件test_scores.txt确实存在。你的解决方案的方式,但我不明白为什么。 – Airfix

+0

它不会抛出异常,但它可能是由于无法预料的事情。阅读和写作东西是更冒险的事情之一。与服务器交谈时,情况变得更糟...... – Flummox

1
InputStream is = getResources().openRawResource(R.raw.yourTxtFile); 
String s = IOUtils.toString(is); // Here is the whole string you want 
IOUtils.closeQuietly(is); // don't forget to close your streams 
+0

除了我的文件是在Assets文件夹而不是资源的原始文件夹。我需要处理资产,以便我可以在用户设备上保存和修改文件。 – Airfix

0

尽量使用try/catch来保持你的应用程序崩溃。他们在那里是有原因的。

这是我的Android之内打开的文件:

try { 
    InputStream inputstream = getResources().openRawResource(getResources().getIndentifier("file_name", "folder", getPackageName())); 
    BufferedReader bufferedRdr = new BufferedReader(inputstream); 
} catch (IOException ioe) { 
    ioe.printStackTrace(); // error handling should be better then this... 
} // and you should close the InputStream and BufferedReader with .close(). 
+0

那么,如果你使用R.raw的参考像David一样建议获得IOException的机会非常低 –

+0

这是使用R.raw的好方法。可能是程序员的首选。如何以及何时处理例外的低可能性。我试图处理它们,所以应用程序不会没有任何明显的原因崩溃。但这是另一个讨论;) – Flummox

+0

我没有在R.raw中使用该文件。我需要访问的文件位于资产文件夹中。这是我的理解,我需要使用assest文件夹,而不是原始文件夹,以允许用户更新和保存数据文件。 – Airfix

0
BufferedReader br = null; 
    try { 
     br = new BufferedReader(new InputStreamReader(getAssets().open("myfile.txt"))); 
     String line; 
     while ((line = br.readLine()) != null) { 
      System.out.println(line); 
     } 
    } catch (IOException e) {  
    } 
相关问题