Java BufferedReader Read All readAll(File input)

Here you can find the source of readAll(File input)

Description

Reads all data from the input stream.

License

Apache License

Parameter

Parameter Description
input the input stream.

Exception

Parameter Description
IOException if the method fails.

Return

the list corresponding to the input stream.

Declaration

private static List<String> readAll(File input) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w ww .j a  v  a2 s .c om
 * Copyright 2011 Andrej Petras <andrej@ajka-andrej.com>.
 *
 * 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.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Reads all data from the input stream.
     * 
     * @param input the input stream.
     * @return the list corresponding to the input stream.
     * 
     * @throws IOException if the method fails.
     */
    private static List<String> readAll(String input) throws IOException {
        List<String> result = new ArrayList<String>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new StringReader(input));
            String line = reader.readLine();
            while (line != null) {
                result.add(line);
                line = reader.readLine();
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        return result;
    }

    /**
     * Reads all data from the input stream.
     * 
     * @param input the input stream.
     * @return the list corresponding to the input stream.
     * 
     * @throws IOException if the method fails.
     */
    private static List<String> readAll(File input) throws IOException {
        List<String> result = new ArrayList<String>();
        BufferedReader reader = null;
        InputStreamReader in = null;
        try {
            in = new FileReader(input);
            reader = new BufferedReader(in);

            String line = reader.readLine();
            while (line != null) {
                result.add(line);
                line = reader.readLine();
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (in != null) {
                in.close();
            }
        }

        return result;
    }
}

Related

  1. readAll(File f)
  2. readAll(File file)
  3. readAll(File file)
  4. readAll(File file)
  5. readAll(final Reader reader)
  6. readAll(InputStream in)
  7. readAll(InputStream in)
  8. readAll(InputStream in, String encoding)