Example usage for org.apache.commons.io FileUtils lineIterator

List of usage examples for org.apache.commons.io FileUtils lineIterator

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils lineIterator.

Prototype

public static LineIterator lineIterator(File file, String encoding) throws IOException 

Source Link

Document

Returns an Iterator for the lines in a File.

Usage

From source file:com.ipcglobal.fredimport.process.Reference.java

/**
 * Creates the ref sexes.//  w  ww .  ja  va 2s  . c  om
 *
 * @param path the path
 * @throws Exception the exception
 */
private void createRefSexes(String path) throws Exception {
    refSexes = new HashMap<String, String>();

    LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_SEXES), "UTF-8");
    try {
        while (it.hasNext()) {
            // Format: <identifier>|<M|F|A>
            // Example: All Persons|A
            String line = it.nextLine();
            String[] fields = line.split("[|]");
            refSexes.put(fields[0].trim(), fields[1].trim());
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:net.mindengine.blogix.web.tiles.TilesContainer.java

private TileLine readAllTileLines(File file) throws IOException {
    LineIterator it = FileUtils.lineIterator(file, "UTF-8");
    /**/*from   ww w. j  a  va2  s  .  co m*/
     * Setting a root tile which will be a container for all tiles
     */
    TileLine rootTileLine = new TileLine();
    rootTileLine.indentation = -1;

    TileLine currentTileLine = rootTileLine;
    try {
        while (it.hasNext()) {
            String line = it.nextLine();
            TileLine tileLine = readKeyValue(currentTileLine, line);
            if (tileLine != null) {
                currentTileLine = tileLine;
            }
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
    return rootTileLine;
}

From source file:com.ipcglobal.fredimport.process.Reference.java

/**
 * Creates the ref us cities states./*from   w w  w .j av a2 s.  com*/
 *
 * @param path the path
 * @throws Exception the exception
 */
private void createRefUSCitiesStates(String path) throws Exception {
    refCitiesByStates = new HashMap<String, String>();

    LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_US_CITIES_STATES), "UTF-8");
    try {
        while (it.hasNext()) {
            // Format: <cities>|<AA-BB-CC>
            String line = it.nextLine();
            String[] fields = line.split("[|]");
            fields[0] = fields[0].trim();
            fields[1] = fields[1].trim();
            refCitiesByStates.put(fields[1], fields[0]);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:fr.ericlab.sondy.core.DataManipulation.java

public StopWords getStopwords(AppVariables appVariables, String name) {
    StopWords stopWords = new StopWords();
    LineIterator it = null;//from w w  w.ja v a 2  s  .  c o m
    try {
        it = FileUtils.lineIterator(new File(appVariables.configuration.getWorkspace() + "/stopwords/" + name),
                "UTF-8");
        while (it.hasNext()) {
            stopWords.add(it.nextLine());
        }
    } catch (IOException ex) {
        Logger.getLogger(DataManipulation.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        LineIterator.closeQuietly(it);
    }
    return stopWords;
}

From source file:ke.co.tawi.babblesms.server.servlet.upload.UploadUtil.java

protected void saveContacts(File contactFile, Account account, ContactDAO contactDAO, PhoneDAO phoneDAO,
        List<String> groupUuids, ContactGroupDAO contactGroupDAO) {
    LineIterator lineIterator = null;//from w ww  . j  a  v a2s.c om
    Contact contact;
    Phone phone;

    List<Group> groupList = new LinkedList<>();
    Group grp;
    for (String uuid : groupUuids) {
        grp = new Group();
        grp.setUuid(uuid);
        groupList.add(grp);
    }

    try {
        lineIterator = FileUtils.lineIterator(contactFile, "UTF-8");

        String line;
        String[] rowTokens, phoneTokens, networkTokens;

        while (lineIterator.hasNext()) {
            line = lineIterator.nextLine();

            rowTokens = StringUtils.split(line, ',');

            // Extract the Contact and save
            contact = new Contact();
            contact.setAccountUuid(account.getUuid());
            contact.setName(rowTokens[0]);
            contact.setStatusUuid(Status.ACTIVE);
            contactDAO.putContact(contact);

            // Extract the phones and save
            phoneTokens = StringUtils.split(rowTokens[1], ';');
            networkTokens = StringUtils.split(rowTokens[2], ';');

            String network;

            for (int j = 0; j < phoneTokens.length; j++) {
                phone = new Phone();
                phone.setPhonenumber(StringUtils.trimToEmpty(phoneTokens[j]));
                phone.setPhonenumber(StringUtils.remove(phone.getPhonenumber(), ' '));
                phone.setContactUuid(contact.getUuid());
                phone.setStatusuuid(Status.ACTIVE);

                network = StringUtils.lowerCase(StringUtils.trimToEmpty(networkTokens[j]));
                phone.setNetworkuuid(networkUuidArray[networkList.indexOf(network)]);

                phoneDAO.putPhone(phone);
            }

            // Associate the Contact to the Groups
            for (Group group : groupList) {
                contactGroupDAO.putContact(contact, group);
            }

        } // end 'while (lineIterator.hasNext())'

    } catch (IOException e) {
        logger.error("IOException when storing: " + contactFile);
        logger.error(e);

    } finally {
        if (lineIterator != null) {
            lineIterator.close();
        }
    }
}

From source file:edu.harvard.med.screensaver.io.libraries.WellDeprecator.java

private static Set<WellKey> readWellsFromFile(CommandLineApplication app) throws IOException, ParseException {
    Set<WellKey> wellKeys = new HashSet<WellKey>();
    LineIterator lines = FileUtils.lineIterator(new File(app.getCommandLineOptionValue("f")), null);
    while (lines.hasNext()) {
        String line = lines.nextLine().trim();
        if (line.length() > 0) {
            try {
                wellKeys.add(new WellKey(line));
            } catch (Exception e) {
                throw new FatalParseException("invalid well key '" + line + "': " + e);
            }/*  w ww . j a  va 2s  .  co m*/
        }
    }
    return wellKeys;
}

From source file:fr.inria.maestro.lga.graph.model.impl.nodenamer.NodeNamerImpl.java

private static int countLines(final File file, final String encoding) throws IOException {
    int count = 0;
    final LineIterator it = FileUtils.lineIterator(file, encoding);
    try {//w w  w  .j a  va  2s .  c o  m
        while (it.hasNext()) {
            it.next();
            count++;
        }
    } finally {
        it.close();
    }

    return count;
}

From source file:de.fhg.iais.asc.ui.MyCortexStarter.java

private LineIterator createLineIterator(String filePath) {
    File f = new File(filePath);
    try {/*w  w  w.ja  v a 2s. co  m*/
        return FileUtils.lineIterator(f, Charsets.UTF_8.name());
    } catch (IOException e) {
        throw new DbcException(
                "Error while starting Cortex Server. Reading \"" + f.getAbsolutePath() + "\" failed", e);
    }
}

From source file:de.teamgrit.grit.checking.testing.JavaProjectTester.java

/**
 * Creates the qualified name form a source file.
 *
 * @param path//  w w  w  . j  av  a  2  s  .c o  m
 *            the path to the sourcefile
 * @return the qualified name
 * @throws IOException
 *             in case of io errors
 */
private String getQuallifiedNameFromSource(Path path) throws IOException {

    final String packageRegex = "package\\s[^,;]+;";
    LineIterator it;
    String result = "";
    it = FileUtils.lineIterator(path.toFile(), "UTF-8");

    while (it.hasNext()) {
        String line = it.nextLine();
        if (line.matches(packageRegex)) {
            result = line;
            // strip not needed elements
            result = result.substring(8, result.length() - 1);
            // append classname
            return result + "." + FilenameUtils.getBaseName(path.toString());
        }
    }
    return FilenameUtils.getBaseName(path.toString());
}

From source file:com.ipcglobal.fredimport.process.Reference.java

/**
 * Creates the ref us states.// w  w w.  j a v a  2  s .  c om
 *
 * @param path the path
 * @throws Exception the exception
 */
private void createRefUSStates(String path) throws Exception {
    refStateAbbrevsByNames = new HashMap<String, String>();
    refValidStateAbbrevs = new HashMap<String, Object>();

    LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_US_STATE_NAMES_ABBREVS), "UTF-8");
    try {
        while (it.hasNext()) {
            // Format: <stateName>|<stateAbbrev>
            String line = it.nextLine();
            String[] fields = line.split("[|]");
            fields[0] = fields[0].trim();
            fields[1] = fields[1].trim();
            refStateAbbrevsByNames.put(fields[0], fields[1]);
            refValidStateAbbrevs.put(fields[1], null);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}