Java Text File Read Line readLines(File f)

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

Description

Read a UTF-8 file into an array of lines.

License

Academic Free License

Declaration

public static String[] readLines(File f) throws IOException 

Method Source Code


//package com.java2s;
// Licensed under the Academic Free License version 3.0

import java.io.*;
import java.util.*;

public class Main {
    /**/*from   ww  w.jav a 2s .  c  o  m*/
     * Read a UTF-8 file into an array of lines.
     */
    public static String[] readLines(File f) throws IOException {
        return readLines(new FileInputStream(f));
    }

    /**
     * Read a UTF-8 file into an array of lines and close the stream.
     */
    public static String[] readLines(InputStream inputStream) throws IOException {
        BufferedReader in = null;
        try {
            ArrayList lines = new ArrayList();
            in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            String line;
            while ((line = in.readLine()) != null)
                lines.add(line);
            return (String[]) lines.toArray(new String[lines.size()]);
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (Exception e) {
            }
        }
    }
}

Related

  1. readLines(File f)
  2. readLines(File file)
  3. readLines(File file)
  4. readLines(File file)