Java File Read getFileContent(final File file)

Here you can find the source of getFileContent(final File file)

Description

Get the content of a file.

License

Open Source License

Parameter

Parameter Description
file file.

Exception

Parameter Description
IOException error while reading.
InterruptedException task cancelled.

Return

its content.

Declaration

public static String getFileContent(final File file) throws IOException, InterruptedException 

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 {
    /**/*  w  w  w  .j  a va  2  s.  c  om*/
     * Get the content of a file.
     * 
     * @param file
     *            file.
     * @return its content.
     * @throws IOException
     *             error while reading.
     * @throws InterruptedException
     *             task cancelled.
     */
    public static String getFileContent(final File file) throws IOException, InterruptedException {
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line = null;
            final StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                // Check for interrupt.
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedException();
                }
                // Read next line.
                sb.append(line);
            }
            return sb.toString();
        }
    }
}

Related

  1. getFileContent(File file, int bufferSize)
  2. getFileContent(File pFile)
  3. getFileContent(final File f)
  4. getFileContent(final File srcFile)
  5. getFileContent(final String fileName)
  6. getFileContent(final String p_filename)
  7. getFileContentAsAString(final File aFile)