Java BufferedReader Read All readAllLines(File file)

Here you can find the source of readAllLines(File file)

Description

Reads all text of a file and returns the lines as string array.

License

Apache License

Parameter

Parameter Description
file The file.

Return

The content of the file as string array.

Declaration

public static String[] readAllLines(File file) 

Method Source Code

//package com.java2s;
/*/*w w  w  .  ja  va2  s .  c o m*/
 * Copyright 2012 BMW Car IT GmbH
 *
 * 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.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Reads all text of a file and returns the lines as string array.
     * 
     * @param file
     *            The file.
     * @return The content of the file as string array.
     */
    public static String[] readAllLines(File file) {
        if (file == null || !file.exists()) {
            throw new IllegalArgumentException("Invalid file given: " + file);
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String line = null;
            List<String> stringBuilder = new ArrayList<String>();
            while ((line = reader.readLine()) != null) {
                stringBuilder.add(trim(line));
            }
            return stringBuilder.toArray(new String[stringBuilder.size()]);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    throw new IllegalStateException(e);
                }
            }
        }
    }

    private static String trim(String str) {
        // trims some invalid characters at the beginning of the string.
        // happens if the templates are in an invalid file format.
        byte[] bytes = str.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i] > 0) {
                // first ok index found
                return new String(bytes, i, bytes.length - i);
            }
        }
        return str;
    }
}

Related

  1. readAllCoursePro()
  2. readAllCourseTime()
  3. readAllFromInputStream(InputStream inputStream)
  4. readAllFromInputStreamAsList(InputStream inputStream)
  5. ReadAllFromStream(InputStream source)
  6. readAllLines(File file)
  7. readAllLines(InputStream inputStream)
  8. readAllLines(InputStream is)
  9. readAllLines(String filepath)