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) throws IOException 

Source Link

Document

Returns an Iterator for the lines in a File using the default encoding for the VM.

Usage

From source file:controllers.FileHandler.java

/**
* brief: read content of a given file to the proper list
* @param strFilePath    - path of given file          e.g. "D:\\EGYETEM\\Szakdolgozat\\Mernoki_tervezes\\update files\\sr28upd\\ADD_NUTR.txt"
* @param strFileType    - type of the given file      e.g. AddFood
* @param TList          - list to fill with data      e.g. List<FileFoodStruct> ListFFS
*///from w  w  w . j  a va2s.c  om
public static <T> void readFile(String strFilePath, List<T> TList, String strFileType) {
    TList.clear();

    try {

        File file = FileUtils.getFile(strFilePath);
        LineIterator iter = FileUtils.lineIterator(file);

        while (iter.hasNext()) {
            String strLine = iter.next();

            switch (strFileType) {
            case "ADD_FOOD": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructFood Object = new FileStructFood(strList.get(0), strList.get(2), strList.get(3),
                        Integer.valueOf(strList.get(8)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "ADD_NUTR": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructNutrient Object = new FileStructNutrient(strList.get(0), strList.get(1),
                        Double.valueOf(strList.get(2)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "ADD_WGT": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructWeight Object = new FileStructWeight(strList.get(0), Double.valueOf(strList.get(2)),
                        strList.get(3), Double.valueOf(strList.get(4)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "CHG_FOOD": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructFood Object = new FileStructFood(strList.get(0), strList.get(2), strList.get(3),
                        Integer.valueOf(strList.get(8)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "CHG_NUTR": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructNutrient Object = new FileStructNutrient(strList.get(0), strList.get(1),
                        Double.valueOf(strList.get(2)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "CHG_WGT": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructWeight Object = new FileStructWeight(strList.get(0), Double.valueOf(strList.get(2)),
                        strList.get(3), Double.valueOf(strList.get(4)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "DEL_FOOD": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructFood Object = new FileStructFood(strList.get(0), strList.get(1));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "DEL_NUTR": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructNutrient Object = new FileStructNutrient(strList.get(0), strList.get(1));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "DEL_WGT": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructWeight Object = new FileStructWeight(strList.get(0), Double.valueOf(strList.get(2)),
                        strList.get(3), Double.valueOf(strList.get(4)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "ADD_NDEF": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructNutrient Object = new FileStructNutrient(strList.get(0), strList.get(1),
                        strList.get(2), strList.get(3), Integer.valueOf(strList.get(4)));
                TList.add((T) Object);
                System.out.println(strList.get(0) + " " + strList.get(1) + " " + strList.get(2) + " "
                        + strList.get(3) + " " + Integer.valueOf(strList.get(4)));
                break;
            }
            case "CHG_NDEF": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructNutrient Object = new FileStructNutrient(strList.get(0), strList.get(1),
                        strList.get(2), strList.get(3), Integer.valueOf(strList.get(4)));
                TList.add((T) Object);
                System.out.println(strList.get(0) + " " + strList.get(1) + " " + strList.get(2) + " "
                        + strList.get(3) + " " + Integer.valueOf(strList.get(4)));
                break;
            }
            case "LOG_FILE": {
                ArrayList<String> strList = ParseLogFile(strLine);
                TraceMessage Object = new TraceMessage(strList.get(0), strList.get(1));
                TList.add((T) Object);
                break;
            }
            }
        }
        iter.close();

    } catch (IOException ex) {
        Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        return;
    }
}

From source file:it.geosolutions.tools.io.file.writer.Writer.java

/**
 * Open 'destination' file in append mode and append content of the
 * 'toAppend' file/*www  .  j  a  v  a  2 s  .c o  m*/
 * 
 * @param toAppend
 * @param destination
 * @throws IOException
 */
public static void appendFile(File toAppend, File destination) throws IOException {
    FileWriter fw = null;
    BufferedWriter bw = null;
    LineIterator it = null;
    try {
        fw = new FileWriter(destination, true);
        bw = new BufferedWriter(fw);
        it = FileUtils.lineIterator(toAppend);
        while (it.hasNext()) {
            bw.append(it.nextLine());
            bw.newLine();
        }
        bw.flush();
    } finally {
        if (it != null) {
            it.close();
        }
        if (bw != null) {
            IOUtils.closeQuietly(bw);
        }
        if (fw != null) {
            IOUtils.closeQuietly(fw);
        }
    }
}

From source file:convert.ConvertStrata.java

@Test
public void convert() throws IOException {
    System.out.println("Convert strata test");
    LineIterator it = FileUtils.lineIterator(
            new File("\\\\delphi\\felles\\alle\\smund\\stox\\strata_norskehavstoktet\\stratum1-6 2014.txt"));
    it.nextLine(); // skip header
    List<Coordinate> coords = new ArrayList<>();
    String strata = null;//from   w w w .  java2s.co m
    while (it.hasNext()) {
        String line = it.nextLine().trim();
        String elms[] = line.split("\t");
        String s = elms[2];
        if (strata != null && !s.equals(strata)) {
            MultiPolygon mp = JTSUtils.createMultiPolygon(Arrays.asList(JTSUtils.createLineString(coords)));
            System.out.println(strata + "\t" + mp);
            coords.clear();
        }
        coords.add(new Coordinate(Conversion.safeStringtoDoubleNULL(elms[1]),
                Conversion.safeStringtoDoubleNULL(elms[0])));
        strata = s;
    }
    MultiPolygon mp = JTSUtils.createMultiPolygon(Arrays.asList(JTSUtils.createLineString(coords)));
    System.out.println(strata + "\t" + mp);
}

From source file:jp.co.cyberagent.parquet.msgpack.JSONLinesIterator.java

public JSONLinesIterator(File file) {
    try {/* ww w.  ja  va2 s. c  o  m*/
        inner = FileUtils.lineIterator(file);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.sangupta.codefix.RightTrim.java

protected String processEachFile(File file) throws IOException {
    final List<String> lines = new ArrayList<String>();
    LineIterator iterator = FileUtils.lineIterator(file);
    boolean modified = false;
    while (iterator.hasNext()) {
        String line = iterator.next();

        String newline = StringUtils.stripEnd(line, null);
        if (line.length() != newline.length()) {
            modified = true;/*  w w w  .  j  a v a2  s  . c om*/
        }

        lines.add(newline);
    }

    FileUtils.writeLines(file, lines);
    return String.valueOf(modified);
}

From source file:com.okmich.hackerday.client.tool.ClientSimulator.java

@Override
public void run() {
    //read file content
    LineIterator lIt;//from   www  .  j  a v a2 s .c om
    try {
        LOG.log(Level.INFO, "Reading the content of file");
        lIt = FileUtils.lineIterator(this.file);
    } catch (IOException ex) {
        Logger.getLogger(ClientSimulator.class.getName()).log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
    String line;
    LOG.log(Level.INFO, "Sending file content to kafka topic - {0}", this.topic);
    while (lIt.hasNext()) {
        line = lIt.nextLine();
        //send message to kafka
        this.kafkaMessageProducer.send(this.topic, line);
        try {
            Thread.currentThread().sleep(this.random.nextInt(1000));
        } catch (InterruptedException ex) {
            Logger.getLogger(ClientSimulator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.hj.blog.common.utils.SensitiveWordMonitor.java

private static Set<String> loadBadWord(File file) {
    Set<String> badWordSet = new HashSet<>();
    try {/*ww  w  . j a v  a 2s. c o m*/
        LineIterator it = FileUtils.lineIterator(file);
        while (it.hasNext()) {
            String badWord = it.nextLine();
            badWordSet.add(badWord);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return badWordSet;
}

From source file:com.sindicetech.siren.demo.bnb.BNBDemo.java

public void index() throws IOException {
    final SimpleIndexer indexer = new SimpleIndexer(indexDir);
    try {//w w w. ja  va2s. com
        int counter = 0;
        final LineIterator it = FileUtils.lineIterator(BNB_PATH);
        while (it.hasNext()) {
            final String id = Integer.toString(counter++);
            final String content = (String) it.next();
            logger.info("Indexing document {}", id);
            indexer.addDocument(id, content);
        }
        LineIterator.closeQuietly(it);
        logger.info("Committing all pending documents");
        indexer.commit();
    } finally {
        logger.info("Closing index");
        indexer.close();
    }
}

From source file:com.sangupta.fileanalysis.formats.base.AbstractDelimitedFileFormatHandler.java

/**
 * Initialize the handler/*from  ww w  .ja va  2s  .c  om*/
 */
@Override
public void initialize(Database database, File file) {
    super.setDBAndFile(database, file);

    try {
        iterator = FileUtils.lineIterator(this.file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:es.ua.dlsi.lexicalinformation.Corpus.java

/**
 * Method that retrieves all the lines containing a given surface form in the
 * corpus./*  ww  w. ja  v  a2  s. c o m*/
 * @param word Word to be searched in the corpus
 * @return Returns the set of lines containing a given surface form in the
 * corpus.
 */
public Set<String> GetAllExamples(String word) {
    Set<String> examples = new LinkedHashSet<String>();
    LineIterator corpus_it = null;
    try {
        corpus_it = FileUtils.lineIterator(new File(this.path));
    } catch (FileNotFoundException ex) {
        System.err.println("Error while trying to open '" + this.path + "' file.");
        System.exit(-1);
    } catch (IOException ex) {
        System.err.println("Error while reading '" + this.path + "' file.");
        System.exit(-1);
    }
    while (corpus_it.hasNext()) {
        String line = corpus_it.nextLine();
        //If the surface form appears in the sentence...
        if (line.matches("^" + word + " .*") || line.matches(".* " + word + "$")
                || line.matches(".* " + word + " .*")) {
            examples.add(line);
        }
    }
    corpus_it.close();
    return examples;
}