这几天在项目开发中,要读取文本文件中内容的,因此写了个读取文本文件中内容的方法,代码如下:

//读取文本文件中的内容
public static String ReadTxtFile(String strFilePath)
{
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 = new FileInputStream(file);
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;
}

更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. 分支和循环(二)(零基础学习C语言)
  3. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  4. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  5. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  6. Android(安卓)Studio 更新 Gradle错误解决方法
  7. Android(安卓)开发常用命令
  8. Android(安卓)setContentView() 详解
  9. Mac中用命令行启动AVD模拟器

随机推荐

  1. android中一个app调用另外一个app中Activ
  2. Android的 animation (总结)
  3. Android自动测试之MonkeyRunner之MonkeyD
  4. Android官方使低版本系统(2.1)支持ActionBa
  5. Android(安卓)找到触摸屏设备节点
  6. ListView的分割线属性操作
  7. 从Bugreport 解读 Android电量统计原理
  8. Android中文API——AudioRecord
  9. 【Android 并发编程】线程间通信的三种基
  10. 源码编译Android 4.1.2的Camera应用(Ecli