Java BufferedReader Read readFileToList(String fileName, List list)

Here you can find the source of readFileToList(String fileName, List list)

Description

read File To List

License

Open Source License

Declaration

public static void readFileToList(String fileName, List<String> list) 

Method Source Code


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

import java.io.Closeable;
import java.io.File;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.List;
import java.util.Map;

public class Main {
    public static void readFileToList(String fileName, List<String> list) {
        readFileToList(new File(fileName), list);
    }//from   w  ww. j  a v a2  s  .  c o  m

    public static void readFileToList(File file, List<String> list) {
        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            String s;
            while ((s = in.readLine()) != null) {
                list.add(s);
            }
            in.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static List<String> readFileToList(String fileName) {
        return readFileToList(new File(fileName));
    }

    public static List<String> readFileToList(File file) {
        List<String> list = new ArrayList<String>();
        readFileToList(file, list);
        return list;
    }

    public static <S, T> void add(Map<S, List<T>> map, S key1, T key2) {
        List<T> s = map.get(key1);
        if (s == null)
            map.put(key1, s = new ArrayList<T>());
        s.add(key2);
    }

    /**
     * Silently close a resource, returning any thrown exception.
     * @param c the resource to close, may be <code>null</code>
     * @return the thrown exception or <code>null</code>
     */
    public static IOException close(Closeable c) {
        try {
            if (c != null)
                c.close();
        } catch (IOException e) {
            return e;
        }
        return null;
    }
}

Related

  1. readFileEncoding(String file)
  2. readFileGdeHash(File gdeFile, ArrayList name, ArrayList seq)
  3. readFilePathToString(String filePath)
  4. readFileToHashMap(String filePath, String separator, boolean valueFirst)
  5. readFileToHashMap(String filePath, String separator, boolean valueFirst)
  6. readFileToList(String path, String split)
  7. readFileToListOfStrings(File file)
  8. readFileValue(File file)
  9. readFileWithoutLineBreak(String filename, boolean skipEmptyLine)