Java Utililty Methods BufferedReader Read

List of utility methods to do BufferedReader Read

Description

The list of methods to do BufferedReader Read are organized into topic(s).

Method

StringreadFile(String fileName)
read File
try (final BufferedReader br = new BufferedReader(new FileReader(fileName))) {
    final StringBuilder sb = new StringBuilder();
    String line = br.readLine();
    while (line != null) {
        sb.append(line);
        sb.append("\n");
        line = br.readLine();
    return sb.toString();
ListreadFile(String filename, boolean print)
read File
List<String> data = new ArrayList<String>();
BufferedReader br = null;
String line = null;
StringBuilder sb = new StringBuilder();
try {
    br = new BufferedReader(new FileReader(filename));
    line = br.readLine();
    if (line != null) {
...
String[]readFile(String filename, boolean skipEmptyLine)
Read file to array of String.
final List<String> result = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(filename));
try {
    String line;
    while ((line = br.readLine()) != null) {
        if (skipEmptyLine && line.trim().length() == 0) {
            continue;
        result.add(line);
} finally {
    br.close();
return (String[]) result.toArray(new String[0]);
ArrayListreadFile(String fileName, String basePath)
read File
ArrayList<String> list = new ArrayList<String>();
BufferedReader result;
try {
    result = new BufferedReader(new FileReader(basePath + fileName));
    while (true) {
        String line = result.readLine();
        if (line == null) {
            break;
...
StringBufferreadFile(String fileName, String commentFlag)
read File
StringBuffer sb = new StringBuffer();
try {
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String s;
    while ((s = in.readLine()) != null) {
        if (commentFlag != null && s.trim().startsWith(commentFlag)) {
            continue;
        sb.append(s);
        sb.append(System.getProperty("line.separator"));
    in.close();
} catch (IOException ex) {
    ex.printStackTrace();
    System.exit(-1);
return sb;
StringBufferreadFile(String fileName, String commentFlag)
Copied from Util.java
StringBuffer sb = new StringBuffer();
try {
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String s;
    while ((s = in.readLine()) != null) {
        if (commentFlag != null && s.trim().startsWith(commentFlag)) {
            continue;
        sb.append(s);
        sb.append(System.getProperty("line.separator"));
    in.close();
} catch (IOException ex) {
    ex.printStackTrace();
return sb;
ArrayListreadFile(String fileNm)
read File
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;
...
StringreadFile(String filePath)
Read file.
StringBuffer stringBuffer = null;
try {
    FileReader fileReader = new FileReader(new File(filePath));
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    stringBuffer = new StringBuffer();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        stringBuffer.append(line);
...
StringreadFile(String filePath)
Method that reads a file and returns the resulting String
StringBuilder contents = new StringBuilder();
try {
    BufferedReader input = new BufferedReader(new FileReader(filePath));
    try {
        String line = null;
        int i = 0;
        while ((line = input.readLine()) != null) {
            if (i > 0)
...
StringreadFile(String filePath)
read File
BufferedReader reader = null;
try {
    reader = new BufferedReader(new FileReader(filePath));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        sb.append(line);
        sb.append("\n");
...