file To Set - Java Collection Framework

Java examples for Collection Framework:Set

Description

file To Set

Demo Code


//package com.java2s;

import java.io.BufferedReader;

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

import java.util.HashSet;

import java.util.Set;

public class Main {
    public static Set<String> fileToSet(File file, boolean toLowerCase,
            boolean trim) {
        try {/*from w w w . ja  v a 2 s.  c  o m*/
            Set<String> set = new HashSet<String>();
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = "";
            while ((line = br.readLine()) != null) {
                if (toLowerCase)
                    set.add(line.toLowerCase());
                else if (trim)
                    set.add(line.trim());
                else
                    set.add(line);
            }
            if (set.isEmpty())
                return null;
            return set;
        } catch (Exception e) {
            return null;
        }
    }
}

Related Tutorials