Skip to content

File Import Export Tools

These classes provide solutions for importing/exporting structures into/from JChem database tables.

Contents

TableImporter

Structure files can be imported into the database using chemaxon.jchem.db.TableImporter.

Java example:

        TableImporter importer = new TableImporter();

        importer.setConnectionHandler(connHandler);
        importer.setInput(fileName);
        importer.setTableName(tableName);
        importer.setLinesToCheck(numOfLines);
        importer.setHaltOnError(false);
        importer.setDuplicatesAllowed(true);

        System.out.println("Collecting file information ...");
        importer.init();
        System.out.println("Done.");

        // Skip "offset" number of structures from the beginning of file
        System.out.println("Skipping " + offset + " structures ...");
        importer.skip(offset);
        System.out.println("Done.");

        // Perform importing
        System.out.println("Importing structures from " + filename + " ...");
        int imported = importer.importMols();
        System.out.println("Done.");
        System.out.println("Imported " + imported + " structures");

Note: For the sake of readability, try/catch is omitted in this example.

Using an InputSream object as data source

You may also use InputStream instead of File in the constructor of TableImporter. The only possible drawback may be that a given number of lines (linesToCheck parameter in the example) will be buffered in memory in that case, so this value shouldn't be excessively high.

TableExporter

With the use of chemaxon.jchem.db.TableExporter, structure table data can be exported into files or any OutputStream objects.

Java example:

        int format = TableTransferConstants.SMILES;
        OutputStream os = new FileOutputStream(new File(fileName));
        TableExporter exporter = new TableExporter();
        exporter.setConnectionHandler(connHandler);
        exporter.setTableName(tableName);
        exporter.setFieldList(fieldString);
        exporter.setOutputStream(os);
        exporter.setFormat(format);
        exporter.setDefaults(true);               // only exporting default fields

        System.out.println("Exporting structures into " + fileName + " ...");
        int written = exporter.writeAll();       // writing file in one step
        System.out.println(written+" molecules were exported.");

Molecules can be written one-by-one, by changing the end of the code to the following:

        int count=0;
        while (exporter.writeNext()) {
            count++;
            System.out.println("Molecule number " + count + " exported.");
        }

Note: For the sake of readability, try/catch is omitted in this example.

Custom select statement

Use TableExporter.setSelectStatement() to specify a custom select statement as the basis of the export. This enables

  • exporting related data from other tables together with the structures
  • ordering the structures with an ORDER BY clause