Java URL Load readURLasString(URL url)

Here you can find the source of readURLasString(URL url)

Description

read UR Las String

License

Open Source License

Exception

Parameter Description
IOException an exception

Return

a with the content of the . Do not use this on long files! Returns null if an error occured or the file doesn't exists.
A newline-character is added at every new line.

Declaration

public static String readURLasString(URL url) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

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

public class Main {
    /**//w w w  .j  a  v a  2 s. c o m
     * @return a {@link String} with the content of the {@link URL}. Do not use
     *         this on long files! Returns <code>null</code> if an error occured
     *         or the file doesn't exists.<br/>
     *         A newline-character is added at every new line.
     * @throws IOException
     */
    public static String readURLasString(URL url) throws IOException {

        InputStream openStream = url.openStream();
        try {

            InputStreamReader inStream = new InputStreamReader(openStream);
            try {

                BufferedReader inReader = new BufferedReader(inStream);
                try {
                    String oneLine = inReader.readLine();
                    if (oneLine == null)
                        return "";
                    StringBuffer content = new StringBuffer();
                    while (oneLine != null) {
                        content.append(oneLine);
                        oneLine = inReader.readLine();
                        if (oneLine != null)
                            content.append("\n");
                    }
                    return content.toString();
                } finally {
                    inReader.close();
                }

            } finally {
                inStream.close();
            }
        } finally {
            openStream.close();
        }
    }
}

Related

  1. readURL(URL url)
  2. readURL(URL url)
  3. readUrl(URL url)
  4. readURL(URL url)
  5. readURL(URL url)
  6. readUrlContent(String address)
  7. readURLContents(String UrlText)
  8. readURLJSONArray(String urlString)
  9. readUrlProperties(URL url)