Java InputStream Read Line readLines(InputStream is)

Here you can find the source of readLines(InputStream is)

Description

read Lines

License

Apache License

Declaration

public static List<String> readLines(InputStream is) 

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 {/*from   w w w  .  j a  v  a  2  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(InputStream is)
  2. readLines(InputStream is)
  3. readLines(InputStream is)
  4. readLines(InputStream is)
  5. readLines(InputStream is)
  6. readLines(InputStream is2, String encoding)
  7. readLines(InputStream is2, String encoding)
  8. readLines(InputStream stream)
  9. readLines(InputStream stream)