Java BufferedReader Read Line readLine(String filePathAndName, String encoding)

Here you can find the source of readLine(String filePathAndName, String encoding)

Description

read the line content of text file, in array
PS. size of element = number of line

License

Open Source License

Parameter

Parameter Description
filePathAndName String file name with absolute path
encoding String file encoding

Return

Array of string content separate by "\n" (new line)

Declaration

public static List<String> readLine(String filePathAndName, String encoding) 

Method Source Code


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

import java.io.*;

import java.util.*;

public class Main {
    public static String DEFAULT_ENCODING = "UTF-8";

    /**// www.  j a  v  a2 s .c  o m
     * read the line content of text file, in array <br>
     * <b>PS.</b> size of element = number of line
     *
     * @param filePathAndName
     *       String file name with absolute path
     * @param encoding
     *       String file encoding
     * @return Array of string content separate by "\n" (new line)
     */
    public static List<String> readLine(String filePathAndName, String encoding) {
        if (encoding == null)
            encoding = DEFAULT_ENCODING;
        String string = "";
        List<String> stringList = new ArrayList<>();
        long i = 0;

        BufferedReader reader = getBuffer(filePathAndName, encoding);
        if (reader == null)
            return stringList;

        try {
            String data;
            while ((data = reader.readLine()) != null) {
                stringList.add(data);
            }
            return stringList;
        } catch (Exception e) {
            return stringList;
        }
    }

    /**
     * read the specified line content of text file
     *
     * @param filePathAndName
     *       String file name with absolute path
     * @param rowIndex
     *       the row number that want to read
     * @return String text content of the line
     */
    public static String readLine(String filePathAndName, long rowIndex) {
        return readLine(filePathAndName, DEFAULT_ENCODING).get(Math.toIntExact(rowIndex));
    }

    /**
     * convert string of file path to bufferReader
     *
     * @param fileAndPath
     *       String file name with absolute path
     * @param encoding
     *       (optional) can be null, Default -> {@link #DEFAULT_ENCODING}
     * @return bufferReader of file
     */
    private static BufferedReader getBuffer(String fileAndPath, String encoding) {
        if (encoding == null || encoding.equals(""))
            encoding = DEFAULT_ENCODING;
        try {
            return new BufferedReader(new InputStreamReader(new FileInputStream(fileAndPath), encoding));
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            return null;
        }
    }
}

Related

  1. readLine(Reader rd)
  2. readLine(String fileName)
  3. readLine(String fileName)
  4. readLine(String filename, String encoding)
  5. readLine(String filepath)
  6. readLine(String fmt, Object... args)
  7. readLine(String path)
  8. readLine(String prompt)
  9. readLine(String prompt, boolean force)