Java BufferedReader Read readFile(final File paramFile, final boolean paramWhitespaces)

Here you can find the source of readFile(final File paramFile, final boolean paramWhitespaces)

Description

Read a file into a StringBuilder.

License

Open Source License

Parameter

Parameter Description
paramFile The file to read.
paramWhitespaces Retrieve file and don't remove any whitespaces.

Exception

Parameter Description
IOException throws an IOException if any I/O operation fails.

Return

StringBuilder instance, which has the string representation of the document.

Declaration

public static StringBuilder readFile(final File paramFile, final boolean paramWhitespaces) throws IOException 

Method Source Code


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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**//from w  w w .  ja  v  a2s  .  c o  m
     * Read a file into a StringBuilder.
     * 
     * @param paramFile
     *            The file to read.
     * @param paramWhitespaces
     *            Retrieve file and don't remove any whitespaces.
     * @return StringBuilder instance, which has the string representation of
     *         the document.
     * @throws IOException
     *             throws an IOException if any I/O operation fails.
     */
    public static StringBuilder readFile(final File paramFile, final boolean paramWhitespaces) throws IOException {
        final BufferedReader in = new BufferedReader(new FileReader(paramFile));
        final StringBuilder sBuilder = new StringBuilder();
        for (String line = in.readLine(); line != null; line = in.readLine()) {
            if (paramWhitespaces) {
                sBuilder.append(line + "\n");
            } else {
                sBuilder.append(line.trim());
            }
        }

        // Remove last newline.
        if (paramWhitespaces) {
            sBuilder.replace(sBuilder.length() - 1, sBuilder.length(), "");
        }
        in.close();

        return sBuilder;
    }
}

Related

  1. readFile(final File file)
  2. readFile(final File file)
  3. readFile(final File file)
  4. readFile(final File file)
  5. readFile(final File file)
  6. readFile(final String file)
  7. readFile(final String fileName)
  8. readFile(final String filename)
  9. readFile(final String fileName)