Java Text File Read Line readLines(File file)

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

Description

Load the content of given File into a string List.

License

Open Source License

Parameter

Parameter Description
file the File

Exception

Parameter Description
IOException an exception
NullPointerException if is is <code>null</code>

Declaration

public static List<String> readLines(File file) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//  w  ww .  j av  a2 s  . c o  m
     * Load the content of <code>InputStream</code> into a string List. The
     * input must be UTF-8 encoded. The method automatically closes the stream
     * after operation is finished.
     * 
     * @param is
     * @return
     * @throws IOException
     * @throws NullPointerException
     *             if is is <code>null</code>
     * @since 6.1
     */
    public static List<String> readLines(InputStream is) throws IOException {
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "UTF-8"));
            List<String> ret = new ArrayList<String>();
            while (reader.ready()) {
                ret.add(reader.readLine());
            }
            return ret;
        } finally {
            is.close();
        }
    }

    /**
     * Load the content of given {@link File} into a string List. The
     * input must be UTF-8 encoded.
     * 
     * @param file the File
     * @return
     * @throws IOException
     * @throws NullPointerException
     *             if is is <code>null</code>
     * @since 6.1
     */
    public static List<String> readLines(File file) throws IOException {
        return readLines(new FileInputStream(file));
    }
}

Related

  1. readLines(File file)
  2. readLines(File file)
  3. readLines(File file)
  4. readLines(File file)
  5. readLines(File file)
  6. readLines(File file)
  7. readLines(File file)
  8. readLines(File file)
  9. readLines(File file)