Java Text File Read Line readLines(String fileName)

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

Description

read Lines

License

Apache License

Declaration

public static List<String> readLines(String fileName) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

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

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

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static List<String> readLines(File file) {
        List<String> fileContent = new ArrayList<String>();
        try {//  w  ww  .j a v a2 s . c  om
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), "utf-8");
                BufferedReader reader = new BufferedReader(read);
                String line;
                while ((line = reader.readLine()) != null) {
                    fileContent.add(line);
                }
                read.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileContent;
    }

    public static List<String> readLines(String fileName) {
        File file = new File(fileName);
        return readLines(file);
    }

    public static List<String> readLines(InputStream is) {
        List<String> fileContent = new ArrayList<String>();
        try {
            InputStreamReader read = new InputStreamReader(is, "utf-8");
            BufferedReader reader = new BufferedReader(read);
            String line;
            while ((line = reader.readLine()) != null) {
                fileContent.add(line);
            }
            read.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileContent;
    }

    public static List<String> readLines(File file, String encoding) {
        List<String> lines = new ArrayList<String>();
        try {
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
                BufferedReader reader = new BufferedReader(read);
                String line;
                while ((line = reader.readLine()) != null) {
                    lines.add(line);
                }
                read.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lines;
    }

    public static List<String> readLines(String fileName, String encoding) {
        File file = new File(fileName);
        return readLines(file, encoding);
    }
}

Related

  1. readLines(String body)
  2. readLines(String file)
  3. readLines(String file)
  4. readLines(String file, ArrayList lines)
  5. readLines(String filename)
  6. readlines(String filename)
  7. readLines(String filename)
  8. readLines(String filename)
  9. readLines(String filename)