2012-06-08 45 views
1

我能夠在SAME活動中編寫然後讀取文本文件,但是我從另一個活動寫入文本文件後無法讀取文本文件。如何在一個活動中寫入文本文件並在另一個活動中讀取該文件?

例:活動A創建並寫入到一個文本文件中。 活動B讀取該文本文件。

我用這個代碼寫入文本文件中活動A

FileOutputStream fos = null; 
     OutputStreamWriter osw = null; 
     try 
     { 
      fos = openFileOutput("user_info.txt", Context.MODE_WORLD_WRITEABLE); 
      osw = new OutputStreamWriter(fos); 
      osw.write("text here"); 
      osw.close(); 
      fos.close(); 
     } 
     catch (FileNotFoundException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

然後,我用這個代碼,試圖讀取活動A創建的同一個文本文件,但我獲得FileNotFoundException

  try 
      { 
       FileInputStream fis = openFileInput("user_info.txt"); 
       InputStreamReader isr = new InputStreamReader(fis); 
       BufferedReader buff = new BufferedReader(isr); 
       String line; 
       while((line = buff.readLine()) != null) 
       { 

        Toast.makeText(this, line, Toast.LENGTH_LONG).show(); 
       } 
      } 
      catch (FileNotFoundException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      catch (IOException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

有誰知道爲什麼我收到了FileNotFoundException

這是路徑問題嗎?

回答

0

不知道你的應用程序是如何構建的,但是,你得到的錯誤看起來像是一個路徑問題,你確定兩個活動都在同一個文件夾中嗎? 如果不是,則需要爲文本文件或相對路徑(如:「../text.txt」)設置一個絕對路徑(如:「/home/user/text.txt」)。 如果您不能確定,嘗試打印使用一些命令對活動的電流通路就像

new File(".").getAbsolutePath(); 

而且,雖然我不能說我是專家與Android,你確定你需要的上下文.MODE_WORLD_WRITEABLE用於您的文件?如果沒有其他應用程序正在讀取或寫入它,它不應該是必要的,對吧?

0

這肯定是一個路徑問題。 你可以寫這樣的

fpath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"yourdirectory"; 

File custdir=new File(fpath); 
if(!custdir.exists()) 
     { 
      custdir.mkdirs(); 

     } 
        File savedir=new File(custdir.getAbsolutePath()); 
        File file = new File(savedir, filename); 

        if(file.exists()) 
        { 
         file.delete(); 
        } 
        FileOutputStream fos; 
        byte[] data = texttosave.getBytes(); 
        try { 
         fos = new FileOutputStream(file); 
         fos.write(data); 
         fos.flush(); 
         fos.close(); 
         Toast.makeText(getBaseContext(), "File Saved", Toast.LENGTH_LONG).show(); 
         finish(); 
        } catch (FileNotFoundException e) { 
         Toast.makeText(getBaseContext(), "Error File Not Found", Toast.LENGTH_LONG).show(); 
         Log.e("fnf", ""+e.getMessage()); 
         // handle exception 
        } catch (IOException e) { 
         // handle exception 
         Toast.makeText(getBaseContext(), "Error IO Exception", Toast.LENGTH_LONG).show(); 
        } 

,你可以讀到這樣

String locatefile=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"yourdirectory"+"/filename"; 




try { 
      br=new BufferedReader(new FileReader(locatefile)); 
        while((text=br.readLine())!=null) 
    { 
     body.append(text); 
     body.append("\n"); 

    } 
} catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 

} catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
相關問題