Java URL Load loadFileOrUrlToList(String iFileOrUrl)

Here you can find the source of loadFileOrUrlToList(String iFileOrUrl)

Description

Reads text from EITHER a remote URL (that starts with http://) OR a local file (doesn't start with http://), into a list of strings, each containing one line of the file.

License

Open Source License

Declaration

public static List<String> loadFileOrUrlToList(String iFileOrUrl) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.URL;

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**/* w  w w .j  av  a2  s.  c  o  m*/
     * Reads text from EITHER a remote URL (that starts with http://) OR a local file (doesn't start with http://), into a list of strings, each containing one line of the file.
     */
    public static List<String> loadFileOrUrlToList(String iFileOrUrl) throws IOException {
        return iFileOrUrl.startsWith("http://") ? loadUrlToList(new URL(iFileOrUrl))
                : loadFileToList(new File(iFileOrUrl));
    }

    /**
     * Reads text from a remote URL into a list of strings, each containing one line of the file.
     */
    public static List<String> loadUrlToList(URL iUrl) throws IOException {
        return loadReaderToList(new InputStreamReader(iUrl.openStream()));
    }

    /**
     * Reads text from a local file into a list of strings, each containing one line of the file.
     */
    public static List<String> loadFileToList(File iFile) throws IOException {
        return loadReaderToList(new FileReader(iFile));
    }

    /**
     * Reads text from an open reader into a list of strings, each containing one line of the file.
     */
    public static List<String> loadReaderToList(Reader reader) throws IOException {
        List<String> outList = new LinkedList<String>();
        String line;
        BufferedReader bufferedReader = new BufferedReader(reader);
        while ((line = bufferedReader.readLine()) != null)
            outList.add(line);
        return outList;
    }
}

Related

  1. loadByteArray(URL url)
  2. loadFile(String url)
  3. loadFile(URL resource)
  4. loadFile(URL url)
  5. loadFileNoArray(URL f)
  6. loadFromUrl(String url)
  7. loadFromURL(URL url)
  8. loadFromURL(URL url)
  9. loadFromURL(URL url)