Java InputStream Read Line readlines(final InputStream stream)

Here you can find the source of readlines(final InputStream stream)

Description

readlines

License

Open Source License

Parameter

Parameter Description
stream the file to read from

Exception

Parameter Description
IOException an exception

Return

the lines in the file

Declaration

public static String[] readlines(final InputStream stream) 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 {
    /**// ww  w.  ja v  a 2 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(final InputStream in)
  2. readLines(final InputStream input, final String encoding)
  3. readLines(final InputStream inputStream)
  4. readLines(final InputStream inputStream)
  5. readLines(final InputStream is)
  6. readLines(InputStream in)
  7. readLines(InputStream in)
  8. readLines(InputStream in)
  9. readLines(InputStream in)