views:

268

answers:

2

I want to read lines from a file into a Set or List. Is there a standard utility for doing this?

If these lines are of the form [key]=[value] I can do:

Properties properties = new Properties();
properties.load(new FileInputStream(file));

The values are single entries, one per line, each value becomes an entry in the Set/List

I know it can be done with LineReader, InputStreams and plenty of boilerplate but I'm looking to avoid this.

+3  A: 

yes, the Properties class itself provides accessor methods for convertions:

public Set<Map.Entry> entrySet()

Returns a KeySet view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

public Set<K> keySet()

Returns a KeySet view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

in alternative you can Enumerate the property using elements()

dfa
Thanks I'd assumed it would fail because the value would be null, it appears that it makes the entry the key with an "=" suffix.One comment on your answer, it looks like the generics are being interpreted as html.
Rich Seller
fixed, thanks :)
dfa
edit to my comment above, the value is the empty string,so the entry's toString() becomes [key]=""
Rich Seller
A: 

If, by standard utility, you mean 3rd-party library, then my answer is: almost!

The Apache Commons is worthy of being a part of almost any Java application. Using IOUtils from the Apache Commons, you can achieve what you describe by doing this:

    final List list = IOUtils.readLines(new FileInputStream(file));
    Set<String> set = new HashSet<String>(list);

The set will contain each line from the file.

Note: IOUtils is not generics-aware, so this code fragment will give a compile warning.

Steve McLeod
Not sure why this has been downvoted, seems like a reasonable answer to me. The advantage of this approach is that you have a Set isn't backed by a Map. The disadvantage is that it won't ignore comments etc in the file so you need to handle them yourself or ensure there are none.
Rich Seller