Example usage for java.io LineNumberReader readLine

List of usage examples for java.io LineNumberReader readLine

Introduction

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

Prototype

public String readLine() throws IOException 

Source Link

Document

Read a line of text.

Usage

From source file:pl.nask.hsn2.service.FileFeederTask.java

private void processWithUri() throws ResourceException, StorageException {
    LineNumberReader reader = openReader();

    try {/*from w ww  . j  a  v  a2  s .com*/
        String line;
        while ((line = reader.readLine()) != null) {
            String lineTrimmed = line.trim();
            if (lineTrimmed.length() != 0) {
                processStringUrl(lineTrimmed);
            }
        }
    } catch (IOException e) {
        throw new ResourceException("Error reading from file: " + uri, e);
    } finally {
        closeReader(reader);
    }
}

From source file:codingchallenge.SortableChallenge.java

private void initProductsMatcher() throws IOException, JSONException {
    List<Product> products = new ArrayList<Product>();
    LineNumberReader lproductsReader = new LineNumberReader(productsReader);
    try {//from  w  w  w  . j  a  v  a  2 s .c o m
        for (String line = lproductsReader.readLine(); line != null; line = lproductsReader.readLine()) {
            line = line.trim();
            if (line.length() == 0) {
                continue;
            }
            JSONTokener tokener = new JSONTokener(line);
            Object token = tokener.nextValue();
            if (!(token instanceof JSONObject)) {
                throw new BadInputException("Bad product data: " + token);
            }
            JSONObject productJSON = (JSONObject) token;
            String name = getStringProp("product_name", productJSON);
            String manufacturer = getStringProp("manufacturer", productJSON);
            String family = getStringProp("family", productJSON);
            String model = getStringProp("model", productJSON);
            String announcedDate = getStringProp("announced-date", productJSON);
            products.add(new Product(name, manufacturer, family, model, announcedDate));
        }
    } finally {
        lproductsReader.close();
    }
    matcher.initProducts(products);

}

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

private void fillMap(final File aFile, final int aLevel) throws IOException {
    final LineNumberReader reader = new LineNumberReader(new FileReader(aFile));
    String line;//from  w ww . java2s  .c  o m
    while ((line = reader.readLine()) != null) {
        final String[] parts = line.split("\t");

        if (parts.length != 2) {
            continue;
        }

        ngramLevelMap.get(aLevel).addSample(parts[0], Long.parseLong(parts[1]));
    }
    reader.close();
}

From source file:de.griffel.confluence.plugins.plantuml.type.UmlSourceBuilder.java

public UmlSourceBuilder append(StringReader stringReader) throws IOException {
    final LineNumberReader reader = new LineNumberReader(stringReader);
    String line = reader.readLine();
    if ("ditaa".equals(line) && DiagramType.UML == diagramType) {
        isDitaa = true;/*from   ww  w  .  jav a  2s .com*/
    }
    while (line != null) {
        appendLine(line);
        line = reader.readLine();
    }
    return this;
}

From source file:pl.nask.hsn2.service.scdbg.ScdbgTool.java

private void processInput(ScdbgResultBuilder builder, InputStream input) throws ResourceException {
    LineNumberReader reader = null;
    try {/*from  w w w.  j  av a2s .c om*/
        reader = new LineNumberReader(new InputStreamReader(input));
        String line = null;
        while ((line = reader.readLine()) != null) {
            LOGGER.debug("Got line: {}", line);
            builder.scanOutputLine(line);
        }
    } catch (IOException e) {
        throw new ResourceException("Error reading output from scdbg", e);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:org.kalypso.model.wspm.ewawi.data.reader.AbstractEwawiReader.java

private void read(final LineNumberReader reader) throws IOException, ParseException {
    while (reader.ready()) {
        final String line = reader.readLine();
        readLine(line);//from  w  w w.j ava 2  s  .co  m
    }
}

From source file:com.autentia.tnt.bill.migration.BillToBillPaymentMigration.java

/**
 * executes an script received by parameter
 * @param script the script to be launched
 *//*from   www .  j ava 2 s  .co m*/
private static void executeScript(String script) throws Exception {
    Connection con = null;
    Statement stmt = null;
    LineNumberReader file = null;
    String delimiter = ";";

    try {
        log.debug("LOADING DATABASE SCRIPT" + script);

        // connect to database
        Class.forName(DATABASE_DRIVER);
        con = DriverManager.getConnection(DATABASE_CONNECTION, DATABASE_USER, DATABASE_PASS); //NOSONAR
        con.setAutoCommit(false); // DATABASE_PASS es nula
        stmt = con.createStatement();

        // load file
        InputStream sqlScript = Thread.currentThread().getContextClassLoader().getResourceAsStream(script);
        if (sqlScript == null) {
            throw new FileNotFoundException(script);
        }
        file = new LineNumberReader(new InputStreamReader(new BufferedInputStream(sqlScript), "UTF-8"));

        // Add batched SQL sentences to statement
        StringBuilder sentence = new StringBuilder();
        String line;
        while ((line = file.readLine()) != null) {
            line = line.trim();
            if (!line.startsWith("--")) {
                // Interpret "DELIMITER" sentences
                if (line.trim().toUpperCase(Locale.ENGLISH).startsWith("DELIMITER")) {
                    delimiter = line.trim().substring("DELIMITER".length()).trim();
                } else {
                    // Add line to sentence
                    if (line.endsWith(delimiter)) {
                        // Remove delimiter
                        String lastLine = line.substring(0, line.length() - delimiter.length());

                        // Append line to sentence
                        sentence.append(lastLine);

                        // Execute sentence
                        log.debug(" " + sentence.toString());
                        stmt.addBatch(sentence.toString());

                        // Prepare new sentence
                        sentence = new StringBuilder();
                    } else {
                        // Append line to sentence
                        sentence.append(line);

                        // Append separator for next line
                        sentence.append(" ");
                    }
                }
            }
        }

        // Execute batch
        log.debug("upgradeDatabase - executing batch of commands");
        stmt.executeBatch();

        // Commit transaction
        log.debug("upgradeDatabase - commiting changes to database");
        con.commit();

        // Report end of migration
        log.debug("END LOADING DATABASE SCRIPT");

    } catch (Exception e) {
        log.error("FAILED: WILL BE ROLLED BACK: ", e);
        if (con != null) {
            con.rollback();
        }

    } finally {
        cierraFichero(file);
        liberaConexion(con, stmt, null);
    }
}

From source file:de.langmi.spring.batch.examples.complex.file.split.GetLineCountTasklet.java

/**
 * Short variant to get the line count of the file, there can be problems with files where
 * the last line has no content.// w ww .j  a  va 2s  . c  o m
 *
 * @param file
 * @param chunkContext
 * @return the line count
 * @throws IOException
 * @throws FileNotFoundException
 */
private int getLineCount(File file) throws IOException, FileNotFoundException {
    LineNumberReader lnr = null;
    try {
        lnr = new LineNumberReader(new FileReader(file));
        String line = null;
        int count = 0;
        while ((line = lnr.readLine()) != null) {
            count = lnr.getLineNumber();
        }
        return count;
    } finally {
        if (lnr != null) {
            lnr.close();
        }
    }
}

From source file:org.kawanfw.sql.jdbc.util.ColPositionBuilder.java

/**
 * /*from   w w  w. ja  va2  s.  com*/
 * Constructor
 * 
 * @param file
 *            the file that contains the positions of the columns
 * @param connectionHttp
 *            TODO
 */
public ColPositionBuilder(File file, ConnectionHttp connectionHttp) throws SQLException {
    if (!file.exists()) {
        String message = Tag.PRODUCT_PRODUCT_FAIL + "Internal File does not exists: " + file;
        throw new SQLException(message, new IOException(message));
    }

    String line = null;
    LineNumberReader lineNumberReader = null;

    try {
        lineNumberReader = new LineNumberReader(new FileReader(file));
        line = lineNumberReader.readLine();
    } catch (FileNotFoundException e) {
        String message = Tag.PRODUCT_PRODUCT_FAIL + "Internal File does not exists: " + file;
        throw new SQLException(message, new IOException(message));
    } catch (IOException e) {
        String message = Tag.PRODUCT_PRODUCT_FAIL + "I/O Error when reading file: " + file;
        throw new SQLException(message, new IOException(message));
    } finally {
        IOUtils.closeQuietly(lineNumberReader);
    }

    if (line == null) {
        String message = Tag.PRODUCT_PRODUCT_FAIL + "Internal File is empty: " + file;
        throw new SQLException(message, new IOException(message));
    }

    line = JsonLineDecryptor.decrypt(line, connectionHttp);

    if (DEBUG) {
        try {
            String fileContent = FileUtils.readFileToString(file);
            debug("fileContent: " + fileContent);
        } catch (IOException e) {
            throw new SQLException(e);
        }
    }

    columnsPosition = JsonColPosition.fromJson(line);
}

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

private void initialize(final File baseDir) throws NumberFormatException, IOException {
    ngramCountMap = new HashMap<Integer, Long>();
    ngramDistinctCountMap = new HashMap<Integer, Long>();

    final File countFile = new File(baseDir, JWeb1TAggregator.AGGREGATED_COUNTS_FILE);
    if (countFile.exists()) {
        final LineNumberReader lineReader = new LineNumberReader(new FileReader(countFile));
        String line;/* w  w  w .j  a v a2 s .  com*/
        while ((line = lineReader.readLine()) != null) {
            final String[] parts = line.split("\t");

            if (parts.length != 3) {
                continue;
            }

            final int ngramSize = Integer.valueOf(parts[0]);
            final long ngramDistinctCount = Long.valueOf(parts[1]);
            final long ngramCount = Long.valueOf(parts[2]);

            ngramCountMap.put(ngramSize, ngramCount);
            ngramDistinctCountMap.put(ngramSize, ngramDistinctCount);
        }
        lineReader.close();
    }

}