Here you can find the source of readLines(InputStream in, Charset cs)
Parameter | Description |
---|---|
in | the InputStream to read from |
cs | the Charset to use. |
Parameter | Description |
---|---|
IOException | if an I/O error was produced while reading the stream. |
List
containing all lines in the file.
public static List<String> readLines(InputStream in, Charset cs) throws IOException
//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); } } }