Java BufferedReader Read Line readLine(File file, int lineNumber)

Here you can find the source of readLine(File file, int lineNumber)

Description

Reads a line from the specified file and lineNumber.

License

Open Source License

Parameter

Parameter Description
file the file to read from.
lineNumber the linenumber to return.

Exception

Parameter Description

Return

a string representing the lineNumber in the file.

Declaration

public static String readLine(File file, int lineNumber) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   www  .  j  a v a 2s.co m*/
 * net/balusc/util/FileUtil.java
 *
 * Copyright (C) 2007 BalusC
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this library.
 * If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;

import java.io.Closeable;
import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.io.Reader;

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Reads a line from the specified file and lineNumber.
     * @param file the file to read from.
     * @param lineNumber the linenumber to return.
     * @return a string representing the lineNumber in the file.
     * @throws java.io.IOException If reading file fails.
     */
    public static String readLine(File file, int lineNumber) throws IOException {
        List<String> records = readRecords(file);
        return records.get(lineNumber);
    }

    public static List<String> readRecords(File file) throws IOException {
        return readRecords(file, true);
    }

    /**
     * Read list of String records from file.
     * @param file The file to read the character writer from.
     * @param includeBlankLines should empty lines be included in the returned list
     * @return A list of String records which represents lines of the file contents.
     * @throws IOException If reading file fails.
     */
    public static synchronized List<String> readRecords(File file, boolean includeBlankLines) throws IOException {
        BufferedReader reader = (BufferedReader) readReader(file);
        List<String> records = new ArrayList<String>();
        String record = null;

        try {
            while ((record = reader.readLine()) != null) {
                boolean isEmpty = record.trim().equals("");
                if ((includeBlankLines && isEmpty) || !isEmpty) {
                    records.add(record);
                }
            }
        } finally {
            close(reader, file);
        }

        return records;
    }

    /**
     * Read character reader from file.
     * @param file The file to read the character reader from.
     * @return The character reader with the file contents (actually: BufferedReader).
     * @throws IOException If reading file fails.
     */
    public static Reader readReader(File file) throws IOException {
        return new BufferedReader(new FileReader(file));
    }

    /**
     * Close the given I/O resource of the given file.
     * @param resource The I/O resource to be closed.
     * @param file The I/O resource's subject.
     */
    private static void close(Closeable resource, File file) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
                String message = "Closing file " + file.getPath() + " failed.";
                // Do your thing with the exception and the message. Print it, log it or mail it.
                System.err.println(message);
                e.printStackTrace();
            }
        }
    }

    /**
     * Close the given I/O resource.
     * @param resource The I/O resource to be closed.
     */
    public static void close(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
                System.err.println("Closing resource failed!");
                e.printStackTrace();
            }
        }
    }
}

Related

  1. readLine(BufferedReader reader)
  2. readLine(BufferedReader reader, StringWriter writer)
  3. readLine(File f)
  4. readLine(File f)
  5. readLine(File file)
  6. readLine(InputStream in)
  7. readLine(InputStream in)
  8. readLine(InputStream input)
  9. readLine(InputStream input)