2011-03-27 93 views
0

所以我意外地收到FileNotFoundException。正如你所看到的,在我調用FileReader之前,我調用了FileInputStream,它工作正常。我試圖將FileReader放在自己的Try/Catch子句中,但收到相同的結果。我已經從這個街區解開了對我的問題不必要的大部分路線。 (最後我打電話LineNumberReader爲好,但我刪除它從塊,因爲我還沒有得到那麼遠。)Android:FileReader意外拋出FileNotFoundException

 String FILENAME = "file.txt"; 
      try { 
        byte[] buffer = new byte[128]; 
        String toStr = new String(); 
        TextView view = (TextView)findViewById(R.id.textview); 
        FileInputStream fis = openFileInput(FILENAME); /////File is found successfully here///// 
        fis.read(buffer); 
        fis.close(); 
        toStr = new String(buffer); 
        view.append(toStr); 
        FileReader fr = new FileReader(FILENAME); /////FileNotFoundExceptionThrownHere///// 
        /////do stuff here///// 
        fr.close(); 
       } 
      catch (FileNotFoundException e) { 
        TextView view = (TextView)findViewById(R.id.textview); 
        view.append("file not found!"); 
       } 
      catch (IOException e) { 
        TextView view = (TextView)findViewById(R.id.textview); 
        view.append("IO error!"); 
      } 

此外,請記住在回答這個我還是有點新手的時候當談到java。我有其他幾種語言的使用經驗,但對我來說,java是一種不同的怪物品種。任何幫助將非常感激!

回答

3

openFileInput()new FileReader()不採用相同的參數。

openFileInput("file.txt")相當於new FileReader(new File(getFilesDir(), "file.txt"))

+0

非常感謝,我爲noob-ish問題表示歉意。我仍然試圖習慣這種語法。 – volcanicLightning 2011-03-30 06:19:17

相關問題