Java File to Text Line fileToLines(String filePath)

Here you can find the source of fileToLines(String filePath)

Description

Converts a File to a List of Strings.

License

Open Source License

Parameter

Parameter Description
filePath a String, the path of the file on the machine

Return

a List of Strings, containing the information of the file at the designated file-path

Declaration

public static List<String> fileToLines(String filePath) 

Method Source Code


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

import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//from   w w  w  . j a v  a2  s  .  c o  m
     * Converts a File to a List of Strings.
     *
     * @param filePath
     *            a String, the path of the file on the machine
     * @return a List of Strings, containing the information of the file at the
     *         designated file-path
     */
    public static List<String> fileToLines(String filePath) {
        List<String> lines = new ArrayList<String>();
        String line = "";
        try {
            BufferedReader in = new BufferedReader(new FileReader(filePath));
            while ((line = in.readLine()) != null) {
                lines.add(line);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }
}

Related

  1. fileToLines(File file)
  2. fileToLines(String filename)
  3. fileToLines(String filename)
  4. fileToLines(String fileName)