我正在编写一个Java程序,用于从包含XML数据的数据库中读取clob列并进行一些处理。
我需要在此 XML 内容上使用正则表达式,因此转换为字符串对我来说是一种有用的方法,并且我确信 clob 的长度不会超过用于操作目的的字符串的容量。截至目前,数据库中大约 4000 个条目中最短和最长的 CLOB 分别为 2000 个字符和 18000 个字符。
我的问题是:对于这种最大长度(例如 20k 个字符),建议使用以下 2 种方法中的哪一种,为什么?
方法1:在Clob上getSubString()
方法,使用更简单:
// cXML is the Clob object
String sXML = cXML.getSubString(1, (int)cXML.length());
方法2: 在方法中使用BufferedReader
和StringBuilder
(更好的性能?
private String ClobToString(Clob data)
throws Exception
{
StringBuilder sb = new StringBuilder();
StringBuilder exDtl = new StringBuilder();
try {
Reader reader = data.getCharacterStream();
BufferedReader br = new BufferedReader(reader);
String line;
while(null != (line = br.readLine())) {
sb.append(line + "n");
}
br.close();
} catch (SQLException e) {
exDtl.append(e.getMessage());
} catch (IOException e) {
exDtl.append(e.getMessage());
}
finally
{
if (exDtl.length() > 0)
{
throw new Exception(exDtl.toString());
}
return sb.toString();
}
} // End Method ClobToString
我曾在一些网站/论坛上读到方法2在性能方面更好,所以我试图理解:
- 在
- Clob 长度的哪个阈值下,建议使用方法 2 而不是方法 1?
- 这两种方法都不推荐吗?如果是,第三种方法是什么?
(我希望避免像StreamFlyer这样的第三方库)
2 的实现存在缺陷,并且由于数据无论如何都不以流式处理方式处理,因此请使用方法 1。方法 1 将调用驱动程序对方法 2 的实现,这可能更准确,可能如下所示:
public static String getString(Clob data) throws SQLException {
StringBuilder sb = new StringBuilder();
char[] cbuf = new char[8192];
int l = 0;
try (Reader r = data.getCharacterStream()) {
while ((l = r.read(cbuf)) > -1) {
sb.append(cbuf, 0, l);
}
} catch (IOException ioe) {
throw new SQLException("Unable to read character stream from Clob.", ioe);
}
return sb.toString();
}
我认为这不会对性能产生任何影响。