Java Reader Read loadFully(String path)

Here you can find the source of loadFully(String path)

Description

Loads text content from the given path.

License

Open Source License

Parameter

Parameter Description
path the path to the file.

Exception

Parameter Description
IOException if I/O error occurred.

Return

the text content.

Declaration

public static String loadFully(String path) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w ww .  j  ava 2s .  c o m*/
 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). 
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:
 *
 */

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

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

import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;

public class Main {
    /**
     * Buffer size that is of good performance in most cases.
     */
    public static final int BUF_SIZE = 0xffff;

    /**
     * Loads text content from the given path.
     * 
     * @param path
     *            the path to the file.
     * @return the text content.
     * @throws IOException
     *             if I/O error occurred.
     */
    public static String loadFully(String path) throws IOException {
        byte[] buf = readBytes(new File(path));
        return new String(buf);
    }

    /**
     * Reads file content.
     * 
     * @param file
     *            the file from where the content will be read.
     * @return the file content in a byte array.
     * @throws IOException
     *             if I/O error occurred.
     */
    public static byte[] readBytes(File file) throws IOException {
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            return readBytes(in);
        } finally {
            close(in);
        }
    }

    /**
     * Reads content from the given stream.
     * 
     * @param in
     *            the input stream to read data from.
     * @return the read content.
     * @throws IOException
     *             if I/O error occurred.
     */
    public static byte[] readBytes(InputStream in) throws IOException {
        try {
            byte[] buf = new byte[BUF_SIZE];
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int read;
            while ((read = in.read(buf)) > 0) {
                out.write(buf, 0, read);
            }
            return out.toByteArray();
        } finally {
            // Close the input stream and return bytes
            close(in);
        }
    }

    /**
     * Closes the streams and resources.
     * 
     * @param resource
     *            to be closed.
     */
    public static void close(Object resource) {
        try {
            if (resource instanceof InputStream) {
                ((InputStream) resource).close();
            } else if (resource instanceof OutputStream) {
                ((OutputStream) resource).close();
            } else if (resource instanceof Reader) {
                ((Reader) resource).close();
            } else if (resource instanceof Writer) {
                ((Writer) resource).close();
            } else if (resource instanceof RandomAccessFile) {
                ((RandomAccessFile) resource).close();
            } else if (resource != null) {
                throw new IllegalArgumentException("Unknown resource: "
                        + resource);
            }
        } catch (IOException e) {
        }
    }
}

Related

  1. getReaderAsString(Reader source)
  2. getReaderContentAsString(Reader reader)
  3. getReaderContents(Reader reader)
  4. load(InputStream input, int bufsize)
  5. loadChars(Reader in)
  6. loadReader(final Reader in, final StringBuffer buffer)
  7. loadReader(java.io.Reader in)
  8. loadResAsString(String res)
  9. loadText(Reader reader, int length)