Java FileInputStream Read readFile(String path)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(String path) 

Method Source Code

//package com.java2s;
/*************************************************************************
*                                                                        *
*  This file is part of the 20n/act project.                             *
*  20n/act enables DNA prediction for synthetic biology/bioengineering.  *
*  Copyright (C) 2017 20n Labs, Inc.                                     *
*                                                                        *
*  Please direct all queries to act@20n.com.                             *
*                                                                        *
*  This program is free software: you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation, either version 3 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
*  This program is distributed in the hope that it will be useful,       *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*  GNU General Public License for more details.                          *
*                                                                        *
*  You should have received a copy of the GNU General Public License     *
*  along with this program.  If not, see <http://www.gnu.org/licenses/>. *
*                                                                        *
*************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

public class Main {
    public static String readFile(String path) {
        File f = new File(path);
        if (!f.isFile())
            return "";

        FileInputStream s;//  w  w  w .java 2  s .co m
        try {
            s = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            return "";
        }

        byte[] content;
        try {
            content = dumpInputStream(s);
        } catch (IOException e) {
            return "";
        }

        return new String(content);
    }

    /**
     * dump an InputStream, returning a newly created byte[] array
     */
    public static byte[] dumpInputStream(InputStream instream) throws IOException {
        int lastindex = 0;
        int increment;
        byte[] A = new byte[2048];
        byte[] B;
        do {
            B = new byte[2 * A.length];
            System.arraycopy(A, 0, B, 0, A.length);
            A = B;
            try {
                increment = instream.read(A, lastindex, A.length - lastindex);
            } catch (IOException e) {
                throw e;
            }
            if (increment == -1)
                /* EOF, no characters read */
                break;
            lastindex += increment;
        } while (lastindex >= A.length);

        /* copy A into B of precisely correct length */
        B = new byte[lastindex];
        System.arraycopy(A, 0, B, 0, B.length);
        return B;
    }
}

Related

  1. readFile(String path)
  2. readFile(String path)
  3. readFile(String path)
  4. readFile(String path)
  5. readFile(String path)
  6. readFile(String path)
  7. readFile(String path, long offset)
  8. readFile(String path, Properties store)
  9. readFile(String path, String encoding)