[英]Issues in reading of data from text file Android Development
我一直在從文本文件讀取數據時遇到問題,該文本文件位於手機的內部存儲器中。 我的代碼如下:
public void recommend(View view) {
Context context = getApplicationContext();
String filename = "SendLog.txt";
TextView tv = (TextView)findViewById(R.id.textView134);
try {
FileInputStream fis = context.openFileInput(filename);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line).append("\n");
tv.getText();
tv.setText(line);
}
} catch (IOException e) {
e.getStackTrace();
}
我沒有任何數據輸入手機。 我的“ SendLog.txt”文件位於/storage/emulated/0/SendLog.txt目錄中。 有人可以幫助我嗎? 我想從文本文件中讀取所有內容並顯示在textView134中。 有人可以協助嗎?
先感謝您!
“ /storage/emulated/0/SendLog.txt”位於外部存儲器而不是內部存儲器上。
例如,您應該使用Environment.getExtenalStorageDirectory()而不是context.openFileInput(filename),
FileInputStream fis = new FileInputStream(new File(Environment.getExtenalStorageDirectory(), filename));
雖然您將需要處理各種可能性,例如外部存儲設備不可用/未安裝。
您還需要清單許可權才能讀取外部存儲,或者如果您打算這樣做也可以替代地寫入外部存儲(您只需要這兩者之一,因為寫意味着讀取)。
您正在使用stringbuilder構建字符串,但使用settexting行。 結果是文件中的最后一行,可能是空行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.