5. File Import/Export Tools

These classes provide solutions for file import and export needs.

Supported file formats:

Importer

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

Java example:

	Importer importer=new Importer();

	importer.setConnectionHandler(conh);
	importer.setInput(fname);
	importer.setTableName(tname);
	importer.setLinesToCheck(nl);
	importer.setHaltOnError(false);
	importer.setDuplicatesAllowed(true);

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

	// this part skips "offset" number of structures from the beginning of file:

	System.out.println("Skipping "+offset+" structures ...");
	importer.skip(offset);
	System.out.println(" Done.");

	System.out.println("Importing structures from "+ filename + " ...");
	int imported=importer.importMols();

	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 Importer. 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.

Exporter

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

Java example:



1	int format=Importer.JTF;
2	File file = new File(fileName);
3       OutputStream os = new FileOutputStream(file);
4	Exporter ex=new Exporter();
5	ex.setConnectionHandler(conh);
6	ex.setTableName(tname);
7	ex.setFieldList(fieldString);
8	ex.setOutputStream(os);
9	ex.setFormat(format);
10	ex.setDefaults(true); // only exporting default fields
11
12	System.out.println("Exporting structures into " + fileName + " ...");
14	int written=ex.writeAll();			//writing file in one step
15	System.out.println(written+" molecules were exported.");

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

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



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

Custom select statement:

Use Exporter.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

     
    Copyright © 1999-2008 ChemAxon Ltd.    All rights reserved.