日期:2014-05-18  浏览次数:20688 次

IO流读取文章的问题
我在struts2的一个项目中,需要读取一篇文章,我数据库里面存放的是文章的路径,有以下代码,在j2se里能正确读取出文章,但是放在struts中就不能读取了。而且封装的方法根本就不执行。请问各位大牛这是怎么回事儿
代码:
public String getArticleContent(String path)
{
File file = new File(path);
String read = "";
StringBuilder sb = new StringBuilder();
InputStreamReader isr = null;
BufferedReader br = null;
try
{
isr = new InputStreamReader(new FileInputStream(file));
br = new BufferedReader(isr);
while ((read = br.readLine()) != null)
{
sb.append(read + "\n");
}
} catch (Exception e)
{
} finally
{
if (br != null)
{
try
{
br.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
if (isr != null)
{
try
{
isr.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
return sb.toString();
}


调用该方法的地方,以及前后代码:
if (res != null)
{
if (res.next())
{
title = res.getString("title");
content = getArticleContent(res
.getString("article_content"));
list.add(new ArticleBean(title, content));
System.out.println("文章路径----->>>"
+ res.getString("article_content"));
System.out.println(content);
} else
{
list.add(new ArticleBean("没有文章", null));
}
}
io流 Java 数据库

------解决方案--------------------
你确定这个 getArticleContent 方法没执行么?这个应该是没有理由的,我估计
是报错了,然后你捕捉到 没有 打印错误信息
  } catch (Exception e)
        {
        } finally。。

还有一个 你的读取文件的逻辑为什么要放在对 ResultSet 的遍历里面,我建议你先拿出来 转成bean ,把数据库连接都关掉,然后在去读文件内容。
------解决方案--------------------