Android Text File Read readLines(String file)

Here you can find the source of readLines(String file)

Description

Generic read line method implementation from a file.

License

Open Source License

Parameter

Parameter Description
file The file to read lines from

Exception

Parameter Description
IOException If there is any error while trying to read from thefile provided

Return

A string array containing the valid lines read or null if there was an error reading the lines

Declaration

private static String[] readLines(String file) throws IOException 

Method Source Code

//package com.java2s;
/**//from w w  w.  j  a v  a2  s .  c  o  m
 * This file is part of SecureNIO. Copyright (C) 2014 K. Dermitzakis
 * <dermitza@gmail.com>
 *
 * SecureNIO is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * SecureNIO is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with SecureNIO. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;

public class Main {
    private static LineNumberReader lr;
    private static FileReader fr;
    private static ArrayList<String> lines;

    /**
     * Generic read line method implementation from a file. This implementation
     * allows empty lines and comment lines beginning with #. The method is used
     * by both {@link #readProtocols(java.lang.String)} and
     * {@link #readCipherSuites(java.lang.String)} and as such, these methods do
     * not distinguish between protocols and cipher suites.
     *
     *
     * @param file The file to read lines from
     * @return A string array containing the valid lines read or null if there
     * was an error reading the lines
     * @throws IOException If there is any error while trying to read from the
     * file provided
     */
    private static String[] readLines(String file) throws IOException {
        String line;
        fr = new FileReader(file);
        lines = new ArrayList<>();
        lr = new LineNumberReader(fr);
        while ((line = lr.readLine()) != null) {
            if (!line.startsWith("#") && !line.isEmpty()) {
                lines.add(line);
            }
        }
        fr.close();
        lr.close();
        String[] ret = lines.toArray(new String[0]);
        lines.clear();
        lines = null;
        fr = null;
        lr = null;
        return ret;
    }
}

Related

  1. readFileAsString(String filename)
  2. readFileLineByLine(File file)
  3. readFree(File file)
  4. readFromFile(File file)
  5. readFull(String path)
  6. readStringFromClasspath(String path, Class c)
  7. readStringFromFile(String fileName)
  8. readText(String path)
  9. readTextFile(String filePath)