---
title: "读取StarDict里的字典文件"
author: "msino"
date: "2009-04-05T20:47:04Z"
tags: [".net", "360", "HTML", "OSGI", "thread"]
slug: "stardict"
draft: false
lastmod: "2009-04-06T14:07:18Z"
id: 361888
url: "https://msino.iteye.com/blog/361888"
---

搜集了些资料，并重新整理了源代码

 

1\. 下载stardict字典文件， 以“langdao-ec-gb dictionary(en - zh\_CN) 朗道英汉字典”为例

 

[http://prdownloads.sourceforge.net/stardict/stardict-langdao-ec-gb-2.4.2.tar.bz2?download](http://prdownloads.sourceforge.net/stardict/stardict-langdao-ec-gb-2.4.2.tar.bz2?download)[](http://stardict.sourceforge.net/Dictionaries_zh_CN.php)

 

2\. 将附件的两个jar包加入到build path中

 

3\. 将附件的config.properties放在你的项目根目录下， 并配置字典路径和字典文件名。如下:

 

dictPath=./stardict-langdao-ec-gb-2.4.2  
dictName=langdao-ec-gb

 

4\. 使用。 看代码:

 

```
package com.msino.dict.test;

import com.msino.dict.StarDictReader;
import com.msino.dict.manager.DictsManager;
import com.msino.dict.service.IDictsManager;
import com.msino.dict.service.core.Word;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Test case of class <code>StarDictReader</code>
 * @author XuDong.Su
 * @version 1.0,  Apr 5, 2009
 * @see com.msino.dict.StarDictReader
 */
public class StarDictReaderTest {
    private StarDictReader instance;
    private IDictsManager dictsManager = new DictsManager();

    private String dictsProp = dictsManager.getDictProps();

    String dictFileDir = "";
    String dictName = "";
	
    /**
     * Default constructor.
     */
    public StarDictReaderTest() {
    	
    	dictFileDir = dictsProp.substring(0, dictsProp.indexOf("||"));
    	dictName = dictsProp.substring(dictsProp.indexOf("||") + 2, dictsProp.length());
    	
    	System.out.println("dictFileDir : " + dictFileDir);
	System.out.println("dictName : " + dictName);
		
		
        try {
            instance =  new StarDictReader(dictFileDir, dictName);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StarDictReaderTest.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
    }
    
    public static void main(String[] args) throws InterruptedException {
    	StarDictReaderTest test = new StarDictReaderTest();
    	test.lookup();
    }

    /**
     * Test of lookup method, of class DictEngine.
     * @throws java.lang.InterruptedException Thread.sleep(long)
     */
    public void lookup() throws InterruptedException {
        System.out.println("lookup");

        String word = "A Programming Language on the 360";
        String word2 = "crystal";
        String word3 = "trailer";
        String word4 = "not exsits";

        try {
            Word result = instance.lookup(word);
            System.out.println(result.self + ": " + result.definition);

            Thread.sleep(1000);

            long start = System.currentTimeMillis();

            System.out.println("Time: " + (start) + "ms");
            result = instance.lookup(word2);
            System.out.println(result.self + ": " + result.definition);

            long end1 = System.currentTimeMillis();
            System.out.println("Time: " + (end1) + "ms");
            System.out.println("Space Time: " + (end1 - start) + "ms");

            result = instance.lookup(word3);
            System.out.println(result.self + ": " + result.definition);
            
            long end2 = System.currentTimeMillis();
            System.out.println("Time: " + (end2 - end1) + "ms");
            
            result = instance.lookup(word4);
            System.out.println(result.self + ": " + result.definition);
            
            long end3 = System.currentTimeMillis();
            System.out.println("Time: " + (end3 - end2) + "ms");
            
        } catch (Exception ex) {
        	System.out.println(ex.getMessage());
            //Logger.getLogger(StarDictReaderTest.class.getName()).log(Level.INFO, ex.getMessage());
        }
    }

}
```