Java InputStreamReader Read readFile(String fileNm)

Here you can find the source of readFile(String fileNm)

Description

read File

License

Open Source License

Declaration

public static ArrayList<String> readFile(String fileNm) 

Method Source Code


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

import java.io.BufferedReader;

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

import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {
    /**/*from w  ww.j  av  a2 s. c  o m*/
     * 
     * @param fileNm
     * @param encode - "utf-8"
     * @return
     */
    public static ArrayList<String> readFile(String fileNm, String encode) {
        ArrayList<String> lines = null;

        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            isr = new InputStreamReader(new FileInputStream(fileNm), encode);
            br = new BufferedReader(isr);
            lines = new ArrayList<String>();

            String line = null;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (isr != null)
                try {
                    isr.close();
                } catch (Exception ex) {
                }
            ;
            if (br != null)
                try {
                    br.close();
                } catch (Exception ex) {
                }
            ;
        }

        return lines;
    }

    public static ArrayList<String> readFile(String fileNm) {
        ArrayList<String> lines = null;

        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(new File(fileNm));
            br = new BufferedReader(fr);
            lines = new ArrayList<String>();

            String line = null;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fr != null)
                try {
                    fr.close();
                } catch (Exception ex) {
                }
            ;
            if (br != null)
                try {
                    br.close();
                } catch (Exception ex) {
                }
            ;
        }

        return lines;
    }
}

Related

  1. readFile(String fileName, String characterEncoding)
  2. readFile(String fileName, String characterEncoding)
  3. readFile(String fileName, String charSet)
  4. readFile(String fileName, String coloredLineIndicator, boolean useSections, boolean isXML)
  5. readFile(String filename1)
  6. readFile(String filePath)
  7. readFile(String filePath)
  8. readFile(String filePath)
  9. readFile(String filePath)