Java BufferedReader Read All readAll(String filePathAndName)

Here you can find the source of readAll(String filePathAndName)

Description

read text file content, return string split by "\n"

License

Open Source License

Parameter

Parameter Description
filePathAndName String file name with absolute path

Return

String text content

Declaration

public static String readAll(String filePathAndName) 

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";

    /**/* w  ww  .jav a 2  s.  c o m*/
     * read all context in file {@link File}
     *
     * @param file
     *       reading file
     * @return String text content
     */
    public static String readAll(File file) {
        return readAll(file.getAbsolutePath(), DEFAULT_ENCODING);
    }

    /**
     * read text file content, return string split by "\n"
     *
     * @param filePathAndName
     *       String file name with absolute path
     * @return String text content
     */
    public static String readAll(String filePathAndName) {
        return readAll(filePathAndName, DEFAULT_ENCODING);
    }

    /**
     * read text file content, return string split by "\n"
     *
     * @param filePathAndName
     *       String file name with absolute path
     * @param encoding
     *       String file encoding
     * @return String text content
     */
    public static String readAll(String filePathAndName, String encoding) {
        return readLine(filePathAndName, encoding).stream().reduce((s, s2) -> s.concat("\n").concat(s2)).orElse("");
    }

    /**
     * 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. readAll(Reader reader)
  2. readAll(Reader reader)
  3. readAll(String fileName)
  4. readAll(String fileName)
  5. readAll(String filePath)
  6. readAll(String ip)
  7. readAll(String path)
  8. ReadAll_Variant2(Reader rd, Writer wr)
  9. readAllCourseNum()