Load a textual input stream and extract all of his lines - Java java.io

Java examples for java.io:InputStream Read

Description

Load a textual input stream and extract all of his lines

Demo Code

//package com.java2s;
import java.io.BufferedReader;

import java.io.FileNotFoundException;

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

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**/*from w  ww. j  a  v a 2 s.co  m*/
     * Load a textual input stream and extract all of his lines
     *
     * @param stream: the stream
     * @return a list of {@link String} representing the file content
     */
    public static List<String> loadTextfromStream(InputStream stream) {

        List<String> list = new LinkedList<String>();

        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(stream));
            String line = reader.readLine();
            while (line != null) {
                list.add(line);
                line = reader.readLine();
            }
            reader.close();
        } catch (FileNotFoundException e) {
        }

        return list;
    }
}

Related Tutorials