Java FileReader Create readTextFile(InputStream in)

Here you can find the source of readTextFile(InputStream in)

Description

Reads the text from the given input stream in the default encoding.

License

Open Source License

Parameter

Parameter Description
in the input stream

Exception

Parameter Description
IOException when stream could not be read.

Return

the text contained in the stream

Declaration

public static String[] readTextFile(InputStream in) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w ww  . j  a v  a  2 s  .c o m
 * Copyright (C) 2010 Viettel Telecom. All rights reserved.
 * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

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

import java.io.FileReader;

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

import java.util.ArrayList;

public class Main {
    /**
     * Reads a text file.
     *
     * @param fileName the name of the text file
     * @return the lines of the text file
     * @throws IOException when file could not be read.
     */
    public static String[] readTextFile(String fileName) throws IOException {
        return readTextFile(new File(fileName));
    }

    /**
     * Reads a text file.
     *
     * @param file the text file
     * @return the lines of the text file
     * @throws IOException when file could not be read.
     */
    public static String[] readTextFile(File file) throws IOException {
        ArrayList lines = new ArrayList();
        BufferedReader in = new BufferedReader(new FileReader(file));

        String line;
        while ((line = in.readLine()) != null) {
            lines.add(line);
        }
        in.close();
        return (String[]) lines.toArray(new String[lines.size()]);
    }

    /**
     * Reads a text file.
     *
     * @param file the text file
     * @param encoding the encoding of the textfile
     * @return the lines of the text file
     * @throws IOException when file could not be read.
     */
    public static String[] readTextFile(File file, String encoding) throws IOException {
        return readTextFile(new FileInputStream(file), encoding);
    }

    /**
     * Reads the text from the given input stream in the default encoding.
     *
     * @param in the input stream
     * @return the text contained in the stream
     * @throws IOException when stream could not be read.
     */
    public static String[] readTextFile(InputStream in) throws IOException {
        return readTextFile(in, null);
    }

    /**
     * Reads the text from the given input stream in the default encoding.
     *
     * @param in the input stream
     * @param encoding the encoding of the textfile
     * @return the text contained in the stream
     * @throws IOException when stream could not be read.
     */
    public static String[] readTextFile(InputStream in, String encoding) throws IOException {
        ArrayList lines = new ArrayList();
        BufferedReader bufferedIn;
        if (encoding == null) {
            bufferedIn = new BufferedReader(new InputStreamReader(in));
        } else {
            bufferedIn = new BufferedReader(new InputStreamReader(in, encoding));
        }
        String line;
        while ((line = bufferedIn.readLine()) != null) {
            lines.add(line);
        }
        bufferedIn.close();
        in.close();
        return (String[]) lines.toArray(new String[lines.size()]);
    }
}

Related

  1. readTextFile(File file)
  2. readTextFile(File file)
  3. readTextFile(File file, boolean newline)
  4. readTextFile(File file, int header)
  5. readTextfile(final String filename)
  6. readTextFile(InputStream in)
  7. readTextFile(String completePath)
  8. readTextFile(String file)
  9. readTextFile(String file)