/**
*****************************************************
* 读取文本文件中的内容
* @方法名称: ReadTxtFile
* @创建时间: 2014-7-17 上午11:27:13
* @参数列表: @param strFilePath 文件名(helloworld.txt)
*****************************************************
*/
public String ReadTxtFile(String strFilePath) {
AssetManager am = HomeActivity.this.getAssets();
String path = strFilePath;
String content = ""; // 文件内容字符串
// 打开文件
File file = new File(path);
// 如果path是传递过来的参数,可以做一个非目录的判断
if (file.isDirectory()) {
Log.d("TestFile", "The File doesn't not exist.");
} else {
try {
InputStream instream = am.open(strFilePath);
if (instream != null) {
InputStreamReader inputreader = new InputStreamReader(
instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
// 分行读取
while ((line = buffreader.readLine()) != null) {
content += line + "\n";
}
instream.close();
}
} catch (java.io.FileNotFoundException e) {
Log.d("TestFile", "The File doesn't not exist.");
} catch (IOException e) {
Log.d("TestFile", e.getMessage());
}
}
return content;
}
读取Assets中文本文件中的内容
最新推荐文章于 2021-05-29 23:45:58 发布