Java Text File Read Line readlines(final File file)

Here you can find the source of readlines(final File file)

Description

readlines

License

Open Source License

Parameter

Parameter Description
file the file to read from

Exception

Parameter Description
IOException an exception

Return

the lines in the file

Declaration

public static String[] readlines(final File file) throws IOException 

Method Source Code

//package com.java2s;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**/* w  w w . ja va2 s  .c  o m*/
     * @param file the file to read from
     * @return the lines in the file
     * @throws IOException
     */
    public static String[] readlines(final File file) throws IOException {
        final BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line = null;
        final List<String> allLines = new ArrayList<String>();
        while ((line = br.readLine()) != null) {
            allLines.add(line);
        }
        br.close();

        return allLines.toArray(new String[allLines.size()]);
    }

    /**
     * @param file the file to read from
     * @param encoding
     * @return the lines in the file
     * @throws IOException
     */
    public static String[] readlines(final File file, String encoding) throws IOException {
        final BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
        String line = null;
        final List<String> allLines = new ArrayList<String>();
        while ((line = br.readLine()) != null) {
            allLines.add(line);
        }
        br.close();

        return allLines.toArray(new String[allLines.size()]);
    }

    /**
     * @param stream the file to read from
     * @return the lines in the file
     * @throws IOException
     */
    public static String[] readlines(final InputStream stream) throws IOException {
        final BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        String line = null;
        final List<String> allLines = new ArrayList<String>();
        while ((line = br.readLine()) != null) {
            allLines.add(line);
        }

        return allLines.toArray(new String[allLines.size()]);
    }

    /**
     * @param stream the file to read from
     * @param encoding the inputstream encoding
     * @return the lines in the file
     * @throws IOException
     */
    public static String[] readlines(final InputStream stream, String encoding) throws IOException {
        final BufferedReader br = new BufferedReader(new InputStreamReader(stream, encoding));
        String line = null;
        final List<String> allLines = new ArrayList<String>();
        while ((line = br.readLine()) != null) {
            allLines.add(line);
        }

        return allLines.toArray(new String[allLines.size()]);
    }
}

Related

  1. readLines(File file, String encoding)
  2. readLines(File file, String outputLineEnding)
  3. readLines(File inputFile)
  4. readLines(File inputFile, String encoding)
  5. readLines(final File file)
  6. readLines(int problemNumber)
  7. readLines(String body)
  8. readLines(String file)
  9. readLines(String file)