Java Text File Read by Charset readLines(InputStream in, Charset cs)

Here you can find the source of readLines(InputStream in, Charset cs)

Description

Read all lines from a InputStream and return them in a java.util.List .

License

Apache License

Parameter

Parameter Description
in the InputStream to read from
cs the Charset to use.

Exception

Parameter Description
IOException if an I/O error was produced while reading the stream.

Return

a List containing all lines in the file.

Declaration

public static List<String> readLines(InputStream in, Charset cs) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w w w. j av a 2s .  c o  m*/
 * Copyright 2013 Netherlands eScience Center
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedReader;

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

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Read all lines from a InputStream and return them in a {@link java.util.List}.
     * 
     * <p>
     * NOTE: <code>in</code> will NOT be explicitly closed once the end of the stream is reached.  
     * </p>
     * 
     * @param in
     *          the InputStream to read from 
     * @param cs
     *          the Charset to use.
     * @return 
     *          a <code>List<String></code> containing all lines in the file.
     * @throws IOException
     *          if an I/O error was produced while reading the stream. 
     */
    public static List<String> readLines(InputStream in, Charset cs) throws IOException {

        ArrayList<String> result = new ArrayList<String>();

        BufferedReader reader = new BufferedReader(new InputStreamReader(in, cs));

        while (true) {
            String line = reader.readLine();

            if (line == null) {
                return result;
            }

            result.add(line);
        }
    }
}

Related

  1. readFileToString(String path, Charset charset)
  2. readFromInputStream(InputStream stream, Charset charset)
  3. readInputStream(InputStream stream, Charset cs)
  4. readInputStreamToString(InputStream instream, Charset charset)
  5. readLineFromStream(InputStream is, StringBuffer buffer, CharsetDecoder decoder)
  6. readLines(InputStream input, Charset charset)
  7. readLines(InputStream is, CharsetDecoder decoder)
  8. readLines(String filePath, Charset charset)
  9. readLines(String path, Charset charset)