Android读取文本文件中内容的方法
2012-11-09 17:25
281 查看
这几天在项目开发中,要读取文本文件中内容的,因此写了个读取文本文件中内容的方法,代码如下:
//读取文本文件中的内容
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;
}
//读取文本文件中的内容
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;
}
相关文章推荐
- Android中读取文本文件中内容的方法
- Android中读取文本文件中内容的方法
- Android中读取文本文件中内容的方法
- 读取Android Assets文本文件中的内容
- Android学习札记10:用SAX解析XML文件时,当标签中出现换行符时可能出现读取内容为空的解决方法
- Android 读取文件内容实现方法总结
- PowerShell读取文本文件指定行内容的方法
- android按行读取文件内容的几个方法
- android按行读取文件内容的几个方法
- Android下一种打开并读取指定路径文件内容的方法
- Android开发实现读取assets目录下db文件的方法示例
- ASP.NET + C#读取ACCESS数据库内容的简单方法
- 解析Android资源文件及他们的读取方法详解
- android读取短信内容 自动填充验证码
- Android应用开发EditText文本内容变化监听方法
- 读取文件内容输出到控制台方法
- 如何读取文本文件的内容?
- Android中TextView中内容不换行的解决方法
- Android 编程时,res/raw文件夹内容大小的限制问题及解决方法
- Java读取文件方法和给文件追加内容