Example usage for java.io LineNumberReader LineNumberReader

List of usage examples for java.io LineNumberReader LineNumberReader

Introduction

In this page you can find the example usage for java.io LineNumberReader LineNumberReader.

Prototype

public LineNumberReader(Reader in) 

Source Link

Document

Create a new line-numbering reader, using the default input-buffer size.

Usage

From source file:com.googlecode.jweb1t.IndexCreator.java

private void read(final File aFile) throws IOException {
    LineNumberReader reader = null;
    try {// ww  w . j a v  a2  s . c o  m
        reader = new LineNumberReader(new FileReader(aFile));
        String line = null;
        String ch = null;

        // first line
        if ((line = reader.readLine()) != null) {
            ch = line.substring(0, 2).trim();
        }

        String old = ch;

        // second line
        while ((line = reader.readLine()) != null) {
            ch = line.substring(0, 2).trim();

            if (!ch.equals(old)) {
                put(old, aFile);
                old = ch;
            }
        }

        put(old, aFile);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:org.axonframework.eventhandling.scheduling.quartz.QuartzTableMaker.java

private void executeSqlScript(String sqlResourcePath) throws DataAccessException {
    EncodedResource resource = new EncodedResource(applicationContext.getResource(sqlResourcePath), "UTF-8");
    List<String> statements = new LinkedList<String>();
    try {/*from   w  w w  .  j  av  a  2s  .co m*/
        LineNumberReader lnr = new LineNumberReader(resource.getReader());
        String script = JdbcTestUtils.readScript(lnr);
        char delimiter = ';';
        if (!JdbcTestUtils.containsSqlScriptDelimiters(script, delimiter)) {
            delimiter = '\n';
        }
        JdbcTestUtils.splitSqlScript(script, delimiter, statements);
        for (String statement : statements) {
            this.entityManager.createNativeQuery(statement).executeUpdate();
        }
    } catch (IOException ex) {
        throw new DataAccessResourceFailureException("Failed to open SQL script '" + sqlResourcePath + "'", ex);
    }
}

From source file:org.axonframework.spring.eventhandling.scheduling.quartz.QuartzTableMaker.java

private void executeSqlScript(String sqlResourcePath) throws DataAccessException {
    EncodedResource resource = new EncodedResource(applicationContext.getResource(sqlResourcePath), "UTF-8");
    List<String> statements = new LinkedList<>();
    try {/*w  w  w .ja v a2  s  .  co  m*/
        LineNumberReader lnr = new LineNumberReader(resource.getReader());
        String script = JdbcTestUtils.readScript(lnr);
        char delimiter = ';';
        if (!JdbcTestUtils.containsSqlScriptDelimiters(script, delimiter)) {
            delimiter = '\n';
        }
        JdbcTestUtils.splitSqlScript(script, delimiter, statements);
        for (String statement : statements) {
            this.entityManager.createNativeQuery(statement).executeUpdate();
        }
    } catch (IOException ex) {
        throw new DataAccessResourceFailureException("Failed to open SQL script '" + sqlResourcePath + "'", ex);
    }
}

From source file:org.kalypso.ogc.sensor.adapter.NativeObservationCSVAdapter.java

@Override
protected List<NativeObservationDataSet> parse(final File source, final TimeZone timeZone,
        final boolean continueWithErrors, final IStatusCollector stati)
        throws FileNotFoundException, IOException {
    final List<NativeObservationDataSet> datasets = new ArrayList<>();

    final DateFormat sdf2 = new SimpleDateFormat("dd MM yy HH mm"); //$NON-NLS-1$
    sdf2.setTimeZone(timeZone);/*from w w  w  . j  a v a  2 s.  c om*/

    final DateFormat sdf4 = new SimpleDateFormat("dd MM yyyy HH mm"); //$NON-NLS-1$
    sdf4.setTimeZone(timeZone);

    final char separator = ';';

    try (final FileReader fileReader = new FileReader(source);
            final LineNumberReader reader = new LineNumberReader(fileReader);
            final CSVReader csv = new CSVReader(reader, separator)) {
        String[] lineIn = null;
        while (ArrayUtils.isNotEmpty(lineIn = csv.readNext())) {
            if (!continueWithErrors && getErrorCount() > getMaxErrorCount())
                return datasets;

            final int lineNumber = reader.getLineNumber();

            try {
                readLine(stati, datasets, sdf2, sdf4, separator, lineIn, lineNumber);
            } catch (final Exception e) {
                stati.add(IStatus.ERROR, String.format(Messages.getString("NativeObservationCSVAdapter_2"), //$NON-NLS-1$
                        lineNumber, e.getLocalizedMessage()));
                tickErrorCount();
            }
        }
    }

    return datasets;
}

From source file:org.kalypso.commons.runtime.LogStatusWrapper.java

/**
 * Constructor: summary is taken from the logfile. The file is parsed and each line which begins with '***' is
 * appended to the summary.//ww  w. ja  va 2 s. c o  m
 */
public LogStatusWrapper(final File logFile, final String charsetName) {
    if (logFile == null)
        throw new IllegalStateException(Messages.getString("org.kalypso.commons.runtime.LogStatusWrapper.1")); //$NON-NLS-1$
    if (!logFile.exists())
        throw new IllegalStateException(
                Messages.getString("org.kalypso.commons.runtime.LogStatusWrapper.2", logFile.toString())); //$NON-NLS-1$

    m_logFile = logFile;
    m_charsetName = charsetName;

    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw);

    LineNumberReader reader = null;
    try {
        reader = new LineNumberReader(
                new InputStreamReader(new BufferedInputStream(new FileInputStream(logFile)), charsetName));
        while (reader.ready()) {
            final String line = reader.readLine();
            if (line == null)
                break;

            if (line.startsWith(SUMMARY_BEGIN_TOKEN) && line.length() > 3)
                pw.println(line.substring(3));
        }

        pw.close();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(reader);
        pw.close();
        m_summary = sw.toString();
    }
}

From source file:org.gephi.io.importer.api.ImportUtils.java

public static LineNumberReader getTextReader(Reader reader) {
    LineNumberReader lineNumberReader = new LineNumberReader(reader);
    return lineNumberReader;
}

From source file:edu.stanford.muse.index.NER.java

public static void readLocationsWG() {
    InputStream is = null;//from   w ww. j  a  v a  2  s  .  c o m
    try {
        is = new GZIPInputStream(NER.class.getClassLoader().getResourceAsStream("WG.locations.txt.gz"));
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is, "UTF-8"));
        while (true) {
            String line = lnr.readLine();
            if (line == null)
                break;
            StringTokenizer st = new StringTokenizer(line, "\t");
            if (st.countTokens() == 4) {
                String locationName = st.nextToken();
                String canonicalName = locationName.toLowerCase();
                if (locationsToSuppress.contains(canonicalName))
                    continue;
                String lat = st.nextToken();
                String longi = st.nextToken();
                String pop = st.nextToken();
                long popl = Long.parseLong(pop);
                float latf = ((float) Integer.parseInt(lat)) / 100.0f;
                float longif = ((float) Integer.parseInt(longi)) / 100.0f;
                Long existingPop = populations.get(canonicalName);
                if (existingPop == null || popl > existingPop) {
                    populations.put(canonicalName, popl);
                    locations.put(canonicalName,
                            new LocationInfo(locationName, Float.toString(latf), Float.toString(longif)));
                }
            }
        }
        if (is != null)
            is.close();
    } catch (Exception e) {
        log.warn("Unable to read World Gazetteer file, places info may be inaccurate");
        log.debug(Util.stackTrace(e));
    }
}

From source file:com.acmeair.web.LoaderREST.java

public void loadFlights(int segments) throws Exception {
    System.out.println("Loading flight data...");
    InputStream csvInputStream = getClass().getResourceAsStream("/mileage.csv");
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(csvInputStream));
    String line1 = lnr.readLine();
    StringTokenizer st = new StringTokenizer(line1, ",");
    ArrayList<AirportCodeMapping> airports = new ArrayList<AirportCodeMapping>();

    // read the first line which are airport names
    while (st.hasMoreTokens()) {
        AirportCodeMapping acm = new AirportCodeMapping();
        acm.setAirportName(st.nextToken());
        airports.add(acm);//from w  w  w .  j ava  2  s . c  om
    }
    // read the second line which contains matching airport codes for the
    // first line
    String line2 = lnr.readLine();
    st = new StringTokenizer(line2, ",");
    int ii = 0;
    while (st.hasMoreTokens()) {
        String airportCode = st.nextToken();
        airports.get(ii).setAirportCode(airportCode);
        ii++;
    }
    // read the other lines which are of format:
    // airport name, aiport code, distance from this airport to whatever
    // airport is in the column from lines one and two
    String line;
    int flightNumber = 0;
    while (true) {
        line = lnr.readLine();
        if (line == null || line.trim().equals("")) {
            break;
        }
        st = new StringTokenizer(line, ",");
        String airportName = st.nextToken();
        String airportCode = st.nextToken();
        if (!alreadyInCollection(airportCode, airports)) {
            AirportCodeMapping acm = new AirportCodeMapping();
            acm.setAirportName(airportName);
            acm.setAirportCode(airportCode);
            airports.add(acm);
        }
        int indexIntoTopLine = 0;
        while (st.hasMoreTokens()) {
            String milesString = st.nextToken();
            if (milesString.equals("NA")) {
                indexIntoTopLine++;
                continue;
            }
            int miles = Integer.parseInt(milesString);
            String toAirport = airports.get(indexIntoTopLine).getAirportCode();
            if (!flightService.getFlightByAirports(airportCode, toAirport).isEmpty()) {
                // already there
                continue;
            }
            String flightId = "AA" + flightNumber;
            FlightSegment flightSeg = new FlightSegment(flightId, airportCode, toAirport, miles);
            flightService.storeFlightSegment(flightSeg);
            Date now = new Date();
            for (int daysFromNow = 0; daysFromNow < segments; daysFromNow++) {
                Calendar c = Calendar.getInstance();
                c.setTime(now);
                c.set(Calendar.HOUR_OF_DAY, 0);
                c.set(Calendar.MINUTE, 0);
                c.set(Calendar.SECOND, 0);
                c.set(Calendar.MILLISECOND, 0);
                c.add(Calendar.DATE, daysFromNow);
                Date departureTime = c.getTime();
                Date arrivalTime = getArrivalTime(departureTime, miles);
                flightService.createNewFlight(flightId, departureTime, arrivalTime, new BigDecimal(500),
                        new BigDecimal(200), 10, 200, "B747");

            }
            flightNumber++;
            indexIntoTopLine++;
        }
    }

    for (int jj = 0; jj < airports.size(); jj++) {
        flightService.storeAirportMapping(airports.get(jj));
    }
    lnr.close();
    System.out.println("Done loading flight data.");
}

From source file:org.kalypso.wspwin.core.WspWinZustand.java

/**
 * Reads content from .str file//from  w  ww .jav  a  2  s.  c o  m
 */
public void read(final File strFile) throws IOException, ParseException {
    LineNumberReader reader = null;
    try {
        reader = new LineNumberReader(new FileReader(strFile));

        final int[] counts = WspWinProfProj.readStrHeader(reader);
        final int profilCount = counts[0];
        final int segmentCount = counts[1];

        final ProfileBean[] profileBeans = ProfileBean.readProfiles(reader, profilCount);
        for (final ProfileBean profileBean : profileBeans)
            addProfile(profileBean);

        final ZustandSegmentBean[] segmentBeans = readZustandSegments(reader, segmentCount, strFile.getName());
        for (final ZustandSegmentBean zustandSegmentBean : segmentBeans)
            addSegment(zustandSegmentBean);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:Transform.java

/**
 * convert a Throwable into an array of Strings
 * @param throwable/*from   w  ww.ja  v  a  2s .  co  m*/
 * @return string representation of the throwable
 */
public static String[] getThrowableStrRep(Throwable throwable) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    throwable.printStackTrace(pw);
    pw.flush();
    LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
    ArrayList<String> lines = new ArrayList<String>();
    try {
        String line = reader.readLine();
        while (line != null) {
            lines.add(line);
            line = reader.readLine();
        }
    } catch (IOException ex) {
        lines.add(ex.toString());
    }
    String[] rep = new String[lines.size()];
    lines.toArray(rep);
    return rep;
}