简体   繁体   中英

How to manage large String

I've been struggling with a large generated string, while trying to save it to a file.

This string comes from com.sap.conn.jco.JCoFunction.toXML(); method.

I'm trying this:

public static void writeXML(JCoFunction jcoFunction, String path){
    File f = new File("C:/XMLFile.xml");

    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        bw.write("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?> ");
        bw.write(jcoFunction.toXML());
        bw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

This should create a xml, approximately 45KB size. But I get a java heap space error. I need help to make a efficient method, to avoid said error.

Thanks in advance!

I don't know about JCoFunction. but, try this code.

private final static int BUFFER_SIZE = 1024;

public static void writeXML(JCoFunction jcoFunction, String path) {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;

    try {
        bis = new BufferedInputStream(new FileInputStream(new File("C:/XMLFile.xml")));
        bos = new BufferedOutputStream(new FileOutputStream(path));

        byte[] buf = new byte[BUFFER_SIZE];
        int len = 0;
        while((len = bis.read(buf)) >= 0) {
            bos.write(buf, 0, len);
        }

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();
    } finally {
        try {
            if(bis != null) bis.close();
            if(bos != null) bos.close();
        } catch (Exception e) {}
    }
}

If the XML data really should be 45KB overall only, your configured heap size is either too small, or you have hit a bug in JCo (eg an internal endless recursive loop).

If not already using the latest JCo patch level, I would install and try this one first. Maybe this potential bug has already been fixed meanwhile. The latest JCo patch level is currently 3.0.16.

If this doesn't help, increase your Java heap space, eg by specifying the JVM option -Xmx1024m (or -Xmx2048m or even larger) when starting your Java program.

If this still doesn't solve the issue, you maybe need to address this to the SAP support.

Additionally you can switch on JCo trace level 8 and check in the JCo traces how much data is really transferred/stored in your JCoFunction instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM