Java URL Load readListFile(URL listFile)

Here you can find the source of readListFile(URL listFile)

Description

Read a file and return the list of lines in an array of strings.

License

Apache License

Parameter

Parameter Description
listFile the url to read from

Return

the lines

Declaration

public static String[] readListFile(URL listFile) throws IOException 

Method Source Code

//package com.java2s;
/*/*  w  ww  . j  a va 2s .c  o  m*/
 * Copyright 2004 Niclas Hedhman
 *
 * 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.util.ArrayList;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class Main {
    /**
     * Read a file and return the list of lines in an array of strings.
     * @param listFile the url to read from
     * @return the lines
     * @exception IOException if a read error occurs
     */
    public static String[] readListFile(URL listFile) throws IOException {
        ArrayList list = new ArrayList();
        InputStream stream = openInputStream(listFile);
        try {
            InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
            BufferedReader reader = new BufferedReader(isr);
            String line = reader.readLine();
            while (line != null) {
                list.add(line);
                line = reader.readLine();
            }
            String[] items = new String[list.size()];
            list.toArray(items);
            return items;
        } finally {
            stream.close();
        }
    }

    private static InputStream openInputStream(URL url) throws IOException {
        try {
            return url.openStream();
        } catch (IOException e) {
            System.out.println("#URL: " + url);
            System.out.println(e.toString());
            throw e;
        }
    }
}

Related

  1. readJsonFromUrl(String urlString)
  2. readLines(final String url)
  3. readLines(final URL url)
  4. readLines(URL url)
  5. readLinesTrimmedNoCommentFromUrl(final URL fileUrl, final String commentString)
  6. readListFile(URL listFile)
  7. readMappingFile(final URL filename)
  8. readModelFromUrl(URL file, Class clazz)
  9. readNetFile(String urlstr)