Here you can find the source of readFromFile(URL source)
Parameter | Description |
---|---|
source | URL to read the contents. |
Parameter | Description |
---|---|
IOException | an exception |
public static String readFromFile(URL source) throws IOException
//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(); } }