Java tutorial
/* Copyright (C) 2005-2012, by the President and Fellows of Harvard College. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Dataverse Network - A web application to share, preserve and analyze research data. Developed at the Institute for Quantitative Social Science, Harvard University. Version 3.0. */ /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package edu.harvard.iq.dvn.ingest.dsb.impl; import java.util.*; import java.util.Scanner; import java.util.logging.*; import java.io.*; import java.io.FileNotFoundException; import org.apache.commons.lang.*; import edu.harvard.iq.dvn.ingest.dsb.*; /** * * @author a.sone */ public class DvnJavaFieldCutter implements FieldCutter { private static Logger dbgLog = Logger.getLogger(DvnJavaFieldCutter.class.getPackage().getName()); public boolean debug = false; public void subsetFile(String infile, String outfile, Set<Integer> columns, Long numCases) { subsetFile(infile, outfile, columns, numCases, "\t"); } public void subsetFile(String infile, String outfile, Set<Integer> columns, Long numCases, String delimiter) { try { subsetFile(new FileInputStream(new File(infile)), outfile, columns, numCases, delimiter); } catch (IOException ex) { throw new RuntimeException("Could not open file " + infile); } } public void subsetFile(InputStream in, String outfile, Set<Integer> columns, Long numCases, String delimiter) { try { Scanner scanner = new Scanner(in); dbgLog.fine("outfile=" + outfile); BufferedWriter out = new BufferedWriter(new FileWriter(outfile)); scanner.useDelimiter("\\n"); for (long caseIndex = 0; caseIndex < numCases; caseIndex++) { if (scanner.hasNext()) { String[] line = (scanner.next()).split(delimiter, -1); List<String> ln = new ArrayList<String>(); for (Integer i : columns) { ln.add(line[i]); } out.write(StringUtils.join(ln, "\t") + "\n"); } else { throw new RuntimeException("Tab file has fewer rows than the determined number of cases."); } } while (scanner.hasNext()) { if (!"".equals(scanner.next())) { throw new RuntimeException( "Tab file has extra nonempty rows than the determined number of cases."); } } scanner.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }