2015-05-18 56 views
0

我是Android新手,我正在使用Android Studio 1.2。Android寫入/讀取拋出FileNotFoundException

所以我用這個代碼寫在一個類文件:

try { 
      //registry is input by user when logging in.. 
       FileOutputStream fOut = openFileOutput(registry+ "d", MODE_APPEND); 
       Toast.makeText(getApplicationContext(), "writing in " + registry+"d the value" + disciplina2, Toast.LENGTH_SHORT).show(); 
       //toast tells me it's writing properly on the correctly named file 
       fOut.write(disciplina2.getBytes()); 
      } catch (FileNotFoundException e) { 
       Toast.makeText(getApplicationContext(), "**not found**", Toast.LENGTH_SHORT).show(); 
      } catch (IOException e) { 
       Toast.makeText(getApplicationContext(), "**io exception**", Toast.LENGTH_SHORT).show(); 
      } 

然後我應該訪問,閱讀並填寫列表我有其他類:

try { 
     InputStream inputstream = this.getAssets().open(registry + "d"); 
     BufferedReader buffer = new BufferedReader(new InputStreamReader(inputstream)); 

     while(buffer.readLine()!=null) { 
      line = buffer.readLine(); 
      listaDisciplina.add(line); 
      Toast.makeText(getApplicationContext(), "discipline " + line, Toast.LENGTH_SHORT).show(); 
     } 
    } 
    catch (FileNotFoundException e) { 
     Toast.makeText(getApplicationContext(), "**not found** " + registry+"d", Toast.LENGTH_SHORT).show(); 
    } catch (IOException e) { 
     Toast.makeText(getApplicationContext(), "**io excep D**", Toast.LENGTH_SHORT).show(); 
    } 

即使文件名稱匹配並且寫入在讀取之前肯定發生,它也會直接進入FileNotFoundException。

有什麼想法?

回答

0

您確定您正在引用正確的文件來讀取寫操作添加的數據嗎?

this.getAssets().open 

getAssets()將會從您的應用程序的「資產」文件夾中的文件

,我認爲你應該使用輸入相應的方法,這將是

this.openFileInput() 
相關問題