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

Exception

Parameter Description
IOException if a read error occurs

Return

the lines

Declaration

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

Method Source Code


//package com.java2s;
/*/*from   ww  w . ja v  a  2 s  .  co  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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;

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
     *
     * @throws IOException if a read error occurs
     */
    public static String[] readListFile(URL listFile) throws IOException {
        ArrayList<String> list = new ArrayList<String>();
        InputStream stream = listFile.openStream();
        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();
        }
    }
}

Related

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