Java URL Load readFromFile(URL source)

Here you can find the source of readFromFile(URL source)

Description

This method takes a URL as parameter to read the contents, and to add into a string buffer.

License

Open Source License

Parameter

Parameter Description
source URL to read the contents.

Exception

Parameter Description
IOException an exception

Return

string, contents of a file specified in the URL source path.

Declaration

public static String readFromFile(URL source) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007, 2008 Symbian Software Limited 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   ww w  .j a  v  a 2 s . c o  m*/
 * Bala Torati (Symbian) - Initial API and implementation
 * Mark Espiritu (VastSystems) - bug 215283
 *******************************************************************************/

import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStreamReader;

import java.net.URL;

import org.eclipse.cdt.core.templateengine.TemplateEngineMessages;

public class Main {
    /**
     * This method takes a URL as parameter to read the contents, and to add
     * into a string buffer.
     * 
     * @param source
     *            URL to read the contents.
     * @return string, contents of a file specified in the URL source path.
     * @throws IOException 
      * 
      * @since 4.0
     */
    public static String readFromFile(URL source) throws IOException {
        char[] chars = new char[4092];
        InputStreamReader contentsReader = null;
        StringBuffer buffer = new StringBuffer();
        if (!new java.io.File(source.getFile()).exists()) {
            throw new FileNotFoundException(
                    TemplateEngineMessages.getString("ProcessHelper.fileNotFound") + source.getFile()); //$NON-NLS-1$
        } else {
            contentsReader = new InputStreamReader(source.openStream());
            int c;
            do {
                c = contentsReader.read(chars);
                if (c == -1)
                    break;
                buffer.append(chars, 0, c);
            } while (c != -1);
            contentsReader.close();
        }
        return buffer.toString();
    }
}

Related

  1. readFileFromURL(URL url)
  2. readFileIntoMap(URL url)
  3. readFileIntoString(URL input)
  4. readFileIntoString(URL url)
  5. readFromFile(URL fileName)
  6. readFromUrl(String url)
  7. readFromUrl(String urlString)
  8. readFromUrl(String urlText)
  9. readFromUrl(URL url)