I have a big CLOB (more than 32kB) that I want to read to a String, using StringBuilder. How do I do this in the most efficient way? I can not use the "int length" constructor for StringBuilder since the lenght of my CLOB is longer than a "int" and needs a "long" value.
I am not that confortable with the Java I/O classes, and would like to get some guidance.
Edit - I have tried with this code for clobToString():
private String clobToString(Clob data) { StringBuilder sb = new StringBuilder(); try { Reader reader = data.getCharacterStream(); BufferedReader br = new BufferedReader(reader); String line; while(null != (line = br.readLine())) { sb.append(line); } br.close(); } catch (SQLException e) { // handle this exception } catch (IOException e) { // handle this exception } return sb.toString(); }
To convert CLOB data type to stringReader r = clob. getCharacterStream(); Read each character one by one from the retrieved Stream of characters and append them to the StringBuilder or StringBuffer.
To read from a CLOB, use the getAsciiStream() or getCharacterStream() method of an oracle. sql. CLOB object to retrieve the entire CLOB as an input stream. The getAsciiStream() method returns an ASCII input stream in a java.
A CLOB (character large object) value can be up to 2,147,483,647 characters long.
Ok I will suppose a general use, first you have to download apache commons, there you will find an utility class named IOUtils which has a method named copy();
Now the solution is: get the input stream of your CLOB object using getAsciiStream() and pass it to the copy() method.
InputStream in = clobObject.getAsciiStream(); StringWriter w = new StringWriter(); IOUtils.copy(in, w); String clobAsString = w.toString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With