Main page | SPSS Writer?
pmStation logo SPSS Reader header

Demo source code

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;

import com.pmstation.spss.reader.SPSSReader;

public class Demo {

  public static void main(String[] args) {
    if (!checkOrShowUsage(args))
      return;
    try {
      TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
      Date start = new Date();
      SPSSReader reader = new SPSSReader(args[0], null);
      Date parsed = new Date();
      Iterator it = reader.getVariables().iterator();
      while (it.hasNext()) {
        System.out.println(it.next());
      }
      Date variablesPrinted = new Date();
      while (reader.read()) {
        for (int i = 0; i < reader.getVariables().size(); i++) {
          Object res = reader.getValue(i);
          if (res instanceof String) {
            System.out.print(((String) res).trim() + " ");
          } else if (res instanceof Date) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z");
            System.out.print(sdf.format(res) + " ");
          } else
            System.out.print(res + " ");
        }
        System.out.println();
      }
      Date finish = new Date();

      System.out.println("Parsing dictionary: "
        + makeTimeDiffString(start, parsed));
      System.out.println("Printing out variables: "
        + makeTimeDiffString(parsed, variablesPrinted));
      System.out.println("Parsing and printing data: "
        + makeTimeDiffString(variablesPrinted, finish));
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private static String makeTimeDiffString(Date start, Date finish) {
    return (finish.getTime() - start.getTime()) + " ms";
  }

  private static boolean checkOrShowUsage(String[] args) {
    if (args.length != 1 || !new File(args[0]).exists()) {
      System.out.println("Usage: java Demo2 ");
      return false;
    } else
      return true;
  }
}
							
 

SPSS Reader, Copyright (c) 2003-2008, pmStation.com