Java Text File Load loadTextFileAsLines(String fileName)

Here you can find the source of loadTextFileAsLines(String fileName)

Description

load Text File As Lines

License

Open Source License

Declaration

public static List<String> loadTextFileAsLines(String fileName) throws IOException 

Method Source Code


//package com.java2s;
/*/*  w  w  w .jav  a  2  s . co m*/
(C) 2007 Stefan Reich (jazz@drjava.de)
This source file is part of Project Prophecy.
For up-to-date information, see http://www.drjava.de/prophecy
    
This source file is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.
*/

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

public class Main {
    public final static String charsetForTextFiles = "UTF8";

    public static List<String> loadTextFileAsLines(String fileName) throws IOException {
        if (!new File(fileName).exists())
            return null;

        FileInputStream fileInputStream = new FileInputStream(fileName);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles);
        List<String> lines = new ArrayList<String>();
        try {
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = reader.readLine()) != null)
                lines.add(line);
        } finally {
            inputStreamReader.close();
        }
        return lines;
    }
}

Related

  1. loadTextFile(String fileFullPath)
  2. loadTextFile(String fileName)
  3. loadTextFile(String filename)
  4. loadTextFile(String fileName)
  5. loadTextFile(String filename, int maxLines)