【发布时间】:2016-11-29 13:53:25
【问题描述】:
下面的代码只调出第一行代码并停止。我想返回每一行代码,直到没有更多代码为止。
private String GetPhoneAddress() {
File directory = Environment.getExternalStorageDirectory();
File myFile = new File(directory, "mythoughtlog.txt");
//File file = new File(Environment.getExternalStorageDirectory() + "mythoughtlog.txt");
if (!myFile.exists()){
String line = "Need to add smth";
return line;
}
String line = null;
//Read text from file
//StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(myFile));
line = br.readLine();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
return line;
}
【问题讨论】:
-
你需要一个循环。你只要求一行。
-
您只阅读了该行一次。您需要像这样添加一个while循环:
while (br.readLine() != null) { line = br.readLine(); },然后将其附加到带有换行符的字符串(此操作也需要在循环内)。 -
@IshitaSinha 那只会显示偶数行。这不是读取文本文件的正确方法。
-
@EJP 看看接受的答案。 :P 这是我提到的方法的详细说明。
标签: java android string file bufferedreader