Java URL Load read(URL file)

Here you can find the source of read(URL file)

Description

read

License

Apache License

Declaration

public static String read(URL file) throws IOException 

Method Source Code

//package com.java2s;
/**//w  w w. j a va 2 s.c  o  m
 * Copyright (C) 2008 Ovea <dev@testatoo.org>
 *
 * 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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;

public class Main {
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    public static String read(URL file) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(file.openStream()));
        try {
            StringWriter sw = new StringWriter();
            copy(input, sw);
            return sw.toString();
        } finally {
            input.close();
        }
    }

    public static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int n;
        while (-1 != (n = in.read(buffer))) {
            out.write(buffer, 0, n);
        }
    }

    public static void copy(Reader input, Writer output) throws IOException {
        char[] buffer = new char[DEFAULT_BUFFER_SIZE];
        int n;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
    }

    public static void copy(InputStream in, File dest) throws IOException {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
        try {
            copy(in, out);
        } finally {
            out.flush();
            out.close();
        }
    }
}

Related

  1. loadURL(URL url)
  2. loadUrlIntoByteArray(String urlString)
  3. loadUrlToList(URL iUrl)
  4. loadURLToString(String url, String EOL)
  5. read(String url)
  6. read(URL url, String charsetName)
  7. readAsString(URL url)
  8. readAsStringUTF8(URL url)
  9. readBytes(URL url)