Java File Read getFileContentsAsString(@Nonnull final File file)

Here you can find the source of getFileContentsAsString(@Nonnull final File file)

Description

Gets the contents of a File as a String .

License

Mozilla Public License

Parameter

Parameter Description
file File to read.

Exception

Parameter Description
IOException If the file does not exist <br />If the file cannot be read <br />If thefile is a directory

Return

contents of the file . String will be empty in case of any errors.

Declaration

@Nonnull
public static String getFileContentsAsString(@Nonnull final File file) throws IOException 

Method Source Code


//package com.java2s;
/* Copyright (c) 2013 dumptruckman
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import static com.google.common.base.Preconditions.checkNotNull;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import javax.annotation.Nonnull;

public class Main {
    private static final int BUFFER_SIZE = 1024;

    /**//w ww  .  ja v  a 2 s . co  m
     * Gets the contents of a {@link File} as a {@link String}.
     *
     * @param file File to read.
     *
     * @return contents of the {@code file}. String will be empty in case of any errors.
     *
     * @throws IOException If the file does not exist <br />If the file cannot be read <br />If the
     * file is a directory
     */
    @Nonnull
    public static String getFileContentsAsString(@Nonnull final File file) throws IOException {
        checkNotNull(file, "file cannot be null.");

        if (!file.exists()) {
            throw new IOException("File " + file + " does not exist");
        }
        if (!file.canRead()) {
            throw new IOException("Cannot read file " + file);
        }
        if (file.isDirectory()) {
            throw new IOException("File " + file + " is directory");
        }

        Writer writer = new StringWriter();
        try (InputStream is = new FileInputStream(file);
                Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
            int numberOfCharsRead;
            char[] buffer = new char[BUFFER_SIZE];
            while ((numberOfCharsRead = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, numberOfCharsRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return writer.toString();
    }
}

Related

  1. getFileContentByLine(String filePath)
  2. getFileContentFromClasspath(String pathToFile)
  3. getFileContentIntoStrCategoriesDictionary( String fileName)
  4. getFileContentLength(String filename)
  5. getFileContentList(String filenamePath)
  6. getFileContentWithoutCRNL(File f, int linesPerElement, int[] setSizes)