Java Text File Read All readAll(final String path)

Here you can find the source of readAll(final String path)

Description

Reads all file.

License

Apache License

Parameter

Parameter Description
path the path to the reading file.

Exception

Parameter Description
IOException if the reading has failed.

Return

the file content.

Declaration

public static final byte[] readAll(final String path) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w ww . ja v a2  s. c om
 * Copyright 2014-2015 SHAF-WORK
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import com.google.common.base.Strings;

public class Main {
    /**
     * Reads all file.
     * 
     * @param path
     *            the path to the reading file.
     * @return the file content.
     * @throws IOException
     *             if the reading has failed.
     */
    public static final byte[] readAll(final String path) throws IOException {
        if (Strings.isNullOrEmpty(path)) {
            throw new IOException(new IllegalArgumentException("The reading file is not defined."));
        }

        return readRange(path, 0, new File(path).length());
    }

    /**
     * Reads the file from the start position to the end position.
     * 
     * @param path
     *            the reading file.
     * @param start
     *            the starting position for reading.
     * @param finish
     *            the finish position for reading.
     * @return the file content.
     * @throws IOException
     *             if the reading has failed.
     */
    public static final byte[] readRange(final String path, final long start, final long finish)
            throws IOException {
        if (path == null) {
            throw new IOException(new IllegalArgumentException("The reading file is not defined: null"));
        }

        if (path.trim().isEmpty()) {
            throw new IOException(new IllegalArgumentException("The reading file is not defined: empty"));
        }

        if (start < 0) {
            throw new IOException(
                    new IllegalArgumentException("The start position for reading is negative: " + start));
        }

        if (finish < 0) {
            throw new IOException(
                    new IllegalArgumentException("The finish position for reading is negative: " + finish));
        }

        if (start > finish) {
            throw new IOException(new IllegalArgumentException(
                    "The start position for reading is greater than finish position: " + start + " > " + finish));
        }

        try (FileInputStream fis = new FileInputStream(path)) {
            fis.skip(start);
            byte[] buf = new byte[(int) (finish - start)];
            fis.read(buf);
            return buf;
        } catch (IOException exc) {
            throw exc;
        }
    }
}

Related

  1. readAll(File file)
  2. readAll(File file)
  3. readAllFile(File file)
  4. readAllFile(String fileName)
  5. readAllText(final String filename)