Java URL Load readFileFromURL(URL url)

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

Description

Reads the content of the file at the given url and returns it.

License

Open Source License

Parameter

Parameter Description
url The url of the file in the bundle.

Return

The content of the file

Declaration

public static String readFileFromURL(URL url) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Obeo.//from  w ww  . ja v a2 s.c o  m
 * 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:
 *     Obeo - initial API and implementation
 *******************************************************************************/

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

public class Main {
    /**
     * Reads the content of the file at the given url and returns it.
     * 
     * @param url
     *            The url of the file in the bundle.
     * @return The content of the file
     */
    public static String readFileFromURL(URL url) {
        String result = ""; //$NON-NLS-1$

        InputStream openStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            openStream = url.openStream();
        } catch (IOException e) {
            // do nothing, openStream is null
        }

        if (openStream != null) {
            inputStreamReader = new InputStreamReader(openStream);
            bufferedReader = new BufferedReader(inputStreamReader);

            StringBuilder stringBuilder = new StringBuilder();

            String line = null;
            try {
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        openStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            result = stringBuilder.toString();
        }

        return result;
    }
}

Related

  1. readData(String url)
  2. readFile(URL file)
  3. readFile(URL url)
  4. readFile(URL url)
  5. readFileContent(URL file)
  6. readFileIntoMap(URL url)
  7. readFileIntoString(URL input)
  8. readFileIntoString(URL url)
  9. readFromFile(URL fileName)