Java BufferedReader Read All readAll(File file)

Here you can find the source of readAll(File file)

Description

Read the complete content of the specified text file.

License

Open Source License

Parameter

Parameter Description
file The file.

Return

Returns the content.

Declaration

public static String readAll(File file) 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  ww  w .  ja  v  a  2s .  c o  m
     * Read the complete content of the specified text file.
     * @param file The file.
     * @return Returns the content.
     */
    public static String readAll(File file) throws IOException {
        char[] buffer = new char[1 << 16];

        StringBuilder content = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(file));
        while (reader.ready()) {
            int count = reader.read(buffer, 0, buffer.length);
            if (count > 0)
                content.append(buffer, 0, count);
        }

        reader.close();

        return content.toString();
    }
}

Related

  1. readAll(File f)
  2. readAll(File file)
  3. readAll(File file)
  4. readAll(File input)
  5. readAll(final Reader reader)
  6. readAll(InputStream in)
  7. readAll(InputStream in)