溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么用java導(dǎo)出dbf文件生僻漢字

發(fā)布時(shí)間:2021-06-24 13:45:28 來源:億速云 閱讀:147 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要講解了“怎么用java導(dǎo)出dbf文件生僻漢字”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用java導(dǎo)出dbf文件生僻漢字”吧!

java導(dǎo)出dbf文件生僻漢字處理

java導(dǎo)出數(shù)據(jù)到dbf文件,如果姓名中有生僻漢字,在dbf中看到的很可能是?號(hào)。

遇到這種情況需查對(duì)GBK的生僻漢字的Unicode表,GBK提及的52個(gè)生僻漢字有兩種Unicode。

例如:

?(yan 3) \u4ADE就不能在dbf中正常顯示是?

如果換成\uE863則可以(可以打開word的插入->符號(hào)->其他符號(hào),在字符代碼中輸入4ADE的到字符插入word,輸入E863的到另一形式插入word,將這兩種形式的字符從word拷貝到Visual Fox Pro的命令窗口可以看到差別,一個(gè)變成?一個(gè)能正常顯示)。

怎么用java導(dǎo)出dbf文件生僻漢字

解決方式:

1.建立52個(gè)生僻漢字的unicode映射Map

2.將生僻漢字轉(zhuǎn)成unicode形式(有可能是將整個(gè)姓名轉(zhuǎn)成unicode)

3.將姓名的unicode形式進(jìn)行分割(\u)生成數(shù)組(注意兩端的雙引號(hào))

4.遍歷unicode數(shù)組,如果找到生僻漢字的unicode則進(jìn)行替換

5.將unicode還原成漢字

6.寫入dbf

漢字轉(zhuǎn)unicode可利用(import com.alibaba.fastjson.JSON) :

//unicode轉(zhuǎn)中文
    public static String unicodeToString(String str) {  
        return String.valueOf(JSON.parse(str));
    }    
    //中文字符轉(zhuǎn)unicode
    public static String stringToUnicode(String s) {  
        return JSON.toJSONString(s, SerializerFeature.BrowserCompatible);
    }

其他說明:

例如:

?在mysql中能顯示出來,導(dǎo)出到dbf中時(shí)如果選擇 字符集為 GB2312或GBK,導(dǎo)出的 ?為?。

在Visual Fox Pro 9的命令窗口里輸入的 ?為?

打開word,插入,輸入字符編碼4DAE得到 ?,插入到word,復(fù)制粘貼到 Visual Fox Pro 9的命令窗口改字顯示 為?

打開word,插入,輸入字符編碼8E63得到 ?,有些版本的Word能顯示出來,有些版本的不能顯示,按Alt+X ,插入到word,復(fù)制粘貼到 Visual Fox Pro 9的命令窗口改字能顯示 正常

怎么用java導(dǎo)出dbf文件生僻漢字

上圖輸入E863無反應(yīng)

怎么用java導(dǎo)出dbf文件生僻漢字

按快捷鍵Alt+x后的效果

java-dbf中文標(biāo)題亂碼

項(xiàng)目中需要對(duì)DBF的文件進(jìn)行導(dǎo)入處理,上網(wǎng)搜了發(fā)現(xiàn)有java-dbf這個(gè)東西。實(shí)際應(yīng)用中踩了不少坑,主要就是中文亂碼的問題。

InputStream in = new FileInputStream("D:\\test.dbf");
        DBFReader reader = new DBFReader(in);
        reader.setCharactersetName("GBK");
        for(int i = 0; i < reader.getFieldCount(); i++){
            DBFField field = reader.getField(i);
            System.out.print(field.getName() + ",");
        }
        System.out.print("\r\n");
        Object[] values;
        while ( (values = reader.nextRecord()) != null ){
            for(Object value : values){
                System.out.print(value.toString() + ",");
            }
            System.out.print("\r\n");
        }

網(wǎng)上寫法千篇一律,大概就是這樣。問題來了DBF中具體數(shù)據(jù)的中文亂碼通過reader.setCharactersetName("GBK")解決了。

但是發(fā)現(xiàn)列名的亂碼還是沒有解決

怎么用java導(dǎo)出dbf文件生僻漢字

后來查了一下源碼,發(fā)現(xiàn)了問題所在

public DBFReader(InputStream in, Charset charset, boolean showDeletedRows) {
        try {
            this.showDeletedRows = showDeletedRows;
            this.inputStream = in;
            this.dataInputStream = new DataInputStream(this.inputStream);
            this.header = new DBFHeader();
            this.header.read(this.dataInputStream, charset, showDeletedRows);
            setCharset(this.header.getUsedCharset());
            /* it might be required to leap to the start of records at times */
            int fieldSize = this.header.getFieldDescriptorSize();
            int tableSize = this.header.getTableHeaderSize();
            int t_dataStartIndex = this.header.headerLength - (tableSize + (fieldSize * this.header.fieldArray.length)) - 1;            
            skip(t_dataStartIndex);
            
            this.mapFieldNames = createMapFieldNames(this.header.userFieldArray);
        } catch (IOException e) {
            DBFUtils.close(dataInputStream);
            DBFUtils.close(in);
            throw new DBFException(e.getMessage(), e);
        }
    }

其中header就是我們讀取的列名,列數(shù)所依靠的成員變量,但是這個(gè)變量在對(duì)象創(chuàng)建的時(shí)候就賦值好了。

這就導(dǎo)致了后來即使調(diào)用了setCharactersetName也解決不了列名亂碼問題。

所以我們要從根本上解決問題,創(chuàng)建對(duì)象的時(shí)候直接傳入charset對(duì)象。

修改后代碼如下

public static void main(String[] args) throws FileNotFoundException {
        InputStream in = new FileInputStream("D:\\test.dbf");
        Charset charset = Charset.forName("GBK");
        DBFReader reader = new DBFReader(in,charset);
        for(int i = 0; i < reader.getFieldCount(); i++){
            DBFField field = reader.getField(i);
            System.out.print(field.getName() + ",");
        }
        System.out.print("\r\n");
        Object[] values;
        while ( (values = reader.nextRecord()) != null ){
            for(Object value : values){
                System.out.print(value.toString() + ",");
            }
            System.out.print("\r\n");
        }
    }

輸出時(shí)候列名就正常了

怎么用java導(dǎo)出dbf文件生僻漢字

感謝各位的閱讀,以上就是“怎么用java導(dǎo)出dbf文件生僻漢字”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么用java導(dǎo)出dbf文件生僻漢字這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI