Java URL Load readFileIntoString(URL input)

Here you can find the source of readFileIntoString(URL input)

Description

Reads the contents of a file into a string variable.

License

Open Source License

Parameter

Parameter Description
input url

Exception

Parameter Description
IOException ioException

Return

string of return

Declaration

public static String readFileIntoString(URL input) throws IOException 

Method Source Code

//package com.java2s;
/*//from   w w  w. j  a va 2  s  . c o  m
 *  IronJacamar, a Java EE Connector Architecture implementation
 *  Copyright 2016, Red Hat Inc, and individual contributors
 *  as indicated by the @author tags. See the copyright.txt file in the
 *  distribution for a full listing of individual contributors.
 *
 *  This is free software; you can redistribute it and/or modify it
 *  under the terms of the Eclipse Public License 1.0 as
 *  published by the Free Software Foundation.
 *
 *  This software 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 Eclipse
 *  Public License for more details.
 *
 *  You should have received a copy of the Eclipse Public License
 *  along with this software; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 *  02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;

public class Main {
    /**
     * Reads the contents of a file into a string variable.
     *
     * @param input url
     * @return string of return
     * @throws IOException ioException
     */
    public static String readFileIntoString(URL input) throws IOException {

        InputStream stream = null;
        InputStreamReader reader = null;
        try {
            stream = input.openStream();
            reader = new InputStreamReader(stream);
            return readStreamIntoString(reader);
        } finally {
            if (reader != null)
                reader.close();
            if (stream != null)
                stream.close();
        }
    }

    /**
     * Reads the contents of a stream into a string variable.
     *
     * @param reader url
     * @return string of return
     * @throws IOException ioException
     */
    private static String readStreamIntoString(Reader reader) throws IOException {
        StringBuilder s = new StringBuilder();
        char a[] = new char[0x10000];
        while (true) {
            int l = reader.read(a);
            if (l == -1)
                break;
            if (l <= 0)
                throw new IOException();
            s.append(a, 0, l);
        }
        return s.toString();
    }
}

Related

  1. readFile(URL url)
  2. readFile(URL url)
  3. readFileContent(URL file)
  4. readFileFromURL(URL url)
  5. readFileIntoMap(URL url)
  6. readFileIntoString(URL url)
  7. readFromFile(URL fileName)
  8. readFromFile(URL source)
  9. readFromUrl(String url)