Android中读取文本文件中内容的方法
2012-02-29 17:05
387 查看
这几天在项目开发中,要读取文本文件中内容的,因此写了个读取文本文件中内容的方法,代码如下:
//读取文本文件中的内容
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下一种打开并读取指定路径文件内容的方法
- Android学习札记10:用SAX解析XML文件时,当标签中出现换行符时可能出现读取内容为空的解决方法
- Android 读取文件内容实现方法总结
- PowerShell读取文本文件指定行内容的方法
- android按行读取文件内容的几个方法
- android按行读取文件内容的几个方法
- 在代码中动态绑定TabHost内容的两种方法(Android)
- android读取资源文件的方法
- C++从文本文件读取数据到vector中的方法
- 读取nutch爬取内容方法
- [Android] 010_打开并显示文本文件的内容
- Android中写文本文件的方法
- java读取文本文件内容
- JavaScript 通过plus读取android手机短信内容
- hibernate读取数据库里内容,session的get方法和load方法的区别
- 如何读取文本文件的内容?