2013-07-24 38 views
-3

我有某幾個txt文件already.I想讀我application.i點擊此按鈕,文件和選擇TXT文件的名稱和閱讀。我該怎麼辦呢? 請幫助我。從txt文件中的Android移動設備

+0

這個問題可能是重複的http://stackoverflow.com/questions/12421814/how-to-read-text-file-in-android的 – snehal

回答

0

您可以使用下面的代碼清單讀取文本的內容文件。

爲了獲取路徑:

File file = app.getFilesDir(); 
String path = file.getAbsoluteFile().getAbsolutePath() + "<filename.extension>"; 

-------------------------------------------------------- 

public static String readAllContents(String path) { 

    String fileContents = null; 

    try { 
     InputStream inputStream = new FileInputStream(path); 
     if (inputStream != null) { 
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
      String receiveString = ""; 
      StringBuilder stringBuilder = new StringBuilder(); 

      while ((receiveString = bufferedReader.readLine()) != null) { 
       stringBuilder.append(receiveString); 
      } 

      inputStream.close(); 
      fileContents = stringBuilder.toString(); 
     } 
    } 

    catch (FileNotFoundException e) { 
     Log.e("exception", "File not found: " + e.toString()); 
    } catch (IOException e) { 
     Log.e("exception", "Can not read file: " + e.toString()); 
    } 

    return fileContents; 
}