4

当我尝试从 XML 文件执行加载时,我觉得 xstream 加载速度达不到我的要求。对于具有 10k ++ 条目的“数据库”,这将需要几分钟。

以下是我用来序列化的整个数据结构。列表(符号和代码)的大小将大约为 10k ++ 个条目。

http://jstock.cvs.sourceforge.net/viewvc/jstock/jstock/src/org/yccheok/jstock/engine/StockCodeAndSymbolDatabase.java?revision=1.11&view=markup

有什么方法我可以尝试,看看它是否会加快我的加载时间?能够仍然加载以前保存的文件也很重要。

以下是用于反序列化的代码。谢谢。

@SuppressWarnings("unchecked")
public static <A> A fromXML(Class c, File file) {
    XStream xStream = new XStream(new DomDriver("UTF-8"));
    InputStream inputStream = null;

    try {
        inputStream = new java.io.FileInputStream(file);
        Object object = xStream.fromXML(inputStream);
        if (c.isInstance(object)) {
            return (A)object;
        }
    }
    catch (Exception exp) {
        log.error(null, exp);
    }
    finally {
        if (false == close(inputStream)) {
        return null;
        }
        inputStream = null;
    }

    return null;
} 
4

1 回答 1

7

避免使用缓慢的 DomDriver。

@SuppressWarnings("unchecked")
public static <A> A fromXML(Class c, File file) {
    // Don't ever try to use DomDriver. They are VERY slow.
    XStream xStream = new XStream();
    InputStream inputStream = null;
    Reader reader = null;

    try {
        inputStream = new java.io.FileInputStream(file);
        reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
        Object object = xStream.fromXML(reader);

        if (c.isInstance(object)) {
            return (A)object;
        }
    }
    catch (Exception exp) {
        log.error(null, exp);
    }
    finally {
        if (false == close(reader)) {
            return null;
        }
        if (false == close(inputStream)) {
            return null;
        }
        reader = null;
        inputStream = null;
    }

    return null;
}
于 2010-09-02T18:07:56.170 回答