【发布时间】:2015-12-29 15:04:19
【问题描述】:
所以我在本地存储中有一个 .txt 文件,它是一个简单的文本文件。文本基本上只是一系列的行。
我正在使用下面的代码尝试读取文本文件(我在调用此方法之前验证文件是否存在)。
public static String GetLocalMasterFileStream(String Operation) throws Exception {
//Get the text file
File file = new File("sdcard/CM3/advices/advice_master.txt");
if (file.canRead() == true) {System.out.println("-----Determined that file is readable");}
//Read text from file
StringBuilder text = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
System.out.println("-----" + line); //for producing test output
text.append('\n');
}
br.close();
System.out.print(text.toString());
return text.toString();
}
代码在日志中产生
----确定文件可读 但那是文件数据不写入日志的唯一输出
我还尝试在 while 循环之前插入以下内容以尝试仅读取第一行
line = br.readLine();
System.out.println("-----" + line);
产生以下输出:
-----null
【问题讨论】:
标签: java android file-io bufferedreader internal-storage