I want to read lines from a file into a Set or List. Is there a standard utility for doing this?
我想将文件中的行读入Set或List。这样做有标准的实用程序吗?
If these lines are of the form [key]=[value] I can do:
如果这些行的形式为[key] = [value],我可以这样做:
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
值是单个条目,每行一个,每个值成为Set / List中的条目
I know it can be done with LineReader, InputStreams and plenty of boilerplate but I'm looking to avoid this.
我知道可以使用LineReader,InputStreams和大量的样板来完成,但我希望避免这种情况。
2 个解决方案
#1
yes, the Properties class itself provides accessor methods for convertions:
是的,Properties类本身为转换提供了访问器方法:
public Set<Map.Entry> entrySet()
public Set
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.
返回此映射中包含的映射的KeySet视图。该集由地图支持,因此对地图的更改将反映在集中,反之亦然。如果在对集合进行迭代时修改了映射(除非通过迭代器自己的remove操作,或者通过迭代器返回的映射条目上的setValue操作),迭代的结果是未定义的。该集支持元素删除,它通过Iterator.remove,Set.remove,removeAll,retainAll和clear操作从地图中删除相应的映射。它不支持add或addAll操作。
public Set<K> keySet()
public Set
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.
返回此映射中包含的键的KeySet视图。该集由地图支持,因此对地图的更改将反映在集中,反之亦然。如果在对集合进行迭代时修改了映射(除了通过迭代器自己的remove操作),迭代的结果是未定义的。该集支持元素删除,它通过Iterator.remove,Set.remove,removeAll,retainAll和clear操作从地图中删除相应的映射。它不支持add或addAll操作。
in alternative you can Enumerate the property using elements()
在替代方案中,您可以使用elements()枚举属性
#2
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:
Apache Commons值得成为几乎所有Java应用程序的一部分。使用Apache Commons中的IOUtils,您可以通过以下方式实现您所描述的内容:
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.
注意:IOUtils不支持泛型,因此此代码片段将发出编译警告。
#1
yes, the Properties class itself provides accessor methods for convertions:
是的,Properties类本身为转换提供了访问器方法:
public Set<Map.Entry> entrySet()
public Set
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.
返回此映射中包含的映射的KeySet视图。该集由地图支持,因此对地图的更改将反映在集中,反之亦然。如果在对集合进行迭代时修改了映射(除非通过迭代器自己的remove操作,或者通过迭代器返回的映射条目上的setValue操作),迭代的结果是未定义的。该集支持元素删除,它通过Iterator.remove,Set.remove,removeAll,retainAll和clear操作从地图中删除相应的映射。它不支持add或addAll操作。
public Set<K> keySet()
public Set
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.
返回此映射中包含的键的KeySet视图。该集由地图支持,因此对地图的更改将反映在集中,反之亦然。如果在对集合进行迭代时修改了映射(除了通过迭代器自己的remove操作),迭代的结果是未定义的。该集支持元素删除,它通过Iterator.remove,Set.remove,removeAll,retainAll和clear操作从地图中删除相应的映射。它不支持add或addAll操作。
in alternative you can Enumerate the property using elements()
在替代方案中,您可以使用elements()枚举属性
#2
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:
Apache Commons值得成为几乎所有Java应用程序的一部分。使用Apache Commons中的IOUtils,您可以通过以下方式实现您所描述的内容:
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.
注意:IOUtils不支持泛型,因此此代码片段将发出编译警告。