file To List - Java Collection Framework

Java examples for Collection Framework:List

Description

file To List

Demo Code


//package com.java2s;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileReader;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<String> fileToList(File file, boolean toLowerCase,
            boolean trim) {
        try {// ww w . ja  va  2  s  .  c  o m
            List<String> list = new ArrayList<String>();
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = "";
            while ((line = br.readLine()) != null) {
                if (toLowerCase)
                    list.add(line.toLowerCase());
                else if (trim)
                    list.add(line.trim());
                else
                    list.add(line);
            }
            return list;
        } catch (Exception e) {
            return null;
        }
    }
}

Related Tutorials