Java URL Load readFile(URL file)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(URL file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2005 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  w  w . j a  va2 s. co  m*/
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

import java.io.BufferedReader;
import java.io.IOException;

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

public class Main {
    public final static int DEFAULT_FILE_SIZE = 15 * 1024;

    public static String readFile(URL file) {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(file.openStream()), DEFAULT_FILE_SIZE);
            StringBuffer buffer = new StringBuffer(DEFAULT_FILE_SIZE);
            char[] readBuffer = new char[2048];
            int n = in.read(readBuffer);
            while (n > 0) {
                buffer.append(readBuffer, 0, n);
                n = in.read(readBuffer);
            }
            return buffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return "";
    }
}

Related

  1. readContents(URL url)
  2. readContentsToArray(URL url)
  3. readContentsToList(URL url)
  4. readContentsToString(URL url)
  5. readData(String url)
  6. readFile(URL url)
  7. readFile(URL url)
  8. readFileContent(URL file)
  9. readFileFromURL(URL url)