[英]japanese-character-encoding-in-java- from Db2 database
我從 db2 獲取 clob 數據並存儲在列表中,然后寫入文本文件,但日語字符沒有被編碼。
從 db2 獲取數據
if (rs != null) {
int slNo = 0;
BufferedReader reader = null;
int counter = 0;
// Read the list of content next
while (rs.next() && !rs.isClosed()) {
// Reserve the first entry in the
// result List for header column
if (counter == 0) {
resultList.add(null);
counter++;
}
reader = new BufferedReader(new InputStreamReader(rs.getAsciiStream(1)));
slNo = rs.getInt(2);
if (slNo == 1) {
resultList.set(0, reader.readLine());
} else {
// result data
resultList.add(reader.readLine());
}}}
寫入文件
FileOutputStream outObjectOutputStream = null;
try {
outObjectOutputStream = new FileOutputStream(absoluteFilePath);
for (String line : resultList) {
if (line != null) {
outObjectOutputStream.write(line.getBytes());
outObjectOutputStream.write("\n".getBytes());
}}'
您必須指定與 DB 中使用的字符編碼相同的字符編碼。 如果表是 Unicode 表,則 DB2 對 CLOB 使用 UTF-8。
如果您已集體讀取 CLOB 的內容(嘗試System.out.println(line);
在將字符串寫入流之前),請嘗試使用line.getBytes("UTF-8");
寫入內容而不是line.getBytes();
. 有關詳細信息,請參閱https://stackoverflow.com/a/12659462/3902663 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.