我正在尝试使用 jdbc java 从 Oracle 表中获取一个 clob 字段并将其写入文件,但有些字符写错了。public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; InputStreamReader st = null; Writer wr = null; try { conn = DataSource.getConnection("URL", "USER", "PASS"); ps = conn.prepareStatement("select fieldClob from tableTest"); rs = ps.executeQuery(); while(rs.next()) { oracle.sql.CLOB clob = (CLOB)rs.getObject("fieldClob"); st = new InputStreamReader(clob.getAsciiStream(),Charset.forName("UTF-8")); int intValueOfChar; StringBuilder buffer = new StringBuilder(); while ((intValueOfChar = st.read()) != -1) { buffer.append((char) intValueOfChar); } wr = new FileWriter("C:/test/file.xml"); wr.write(buffer.toString()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { CloseObject.closeObject(rs); CloseObject.closeObject(ps); CloseObject.closeObject(conn); }}}我想得到“ü”但我得到“[xBF]”或者我得到“?” 尝试 UTF-8 字符集。
1 回答
一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
更改了 Writer 并添加了对 UTF8 的支持:
Writer fstream = new OutputStreamWriter(new FileOutputStream(mergedFile), StandardCharsets.UTF_8);
添加回答
举报
0/150
提交
取消