如何创建读取制表符分隔文件的通用进程,并从中返回通用映射
我工作的几乎每个项目都会有某种制表符分隔的文件,我需要读入并返回它的查找映射。我发现自己一遍又一遍地重写相同的方法。我想创建一些更通用的东西,这样我就不必一直复制和粘贴代码。在下面的代码中,我只更改了第 9 行和 16-19。所以我只改变Map
,以及我想要如何将数据输入到Map中的实现。有没有办法从中创建一个通用过程,所以每次我想调用这个方法时,我所需要的就是提供我想要如何将数据输入到 Map 中的实现,并以某种方式将 Map 更改为更通用的类型,如出色地。
1. public Map<String, PackageLog> readAllLogsIntoMap(File file){
2. if (!file.exists())
3. {
4. return new HashMap <String, PackageLog> ();
5. }
6. BufferedReader reader = null;
7. FileReader fileReader = null;
8. String data = null;
9. Map <String, PackageLog> resultMap = new HashMap <String, PackageLog> ();
10. try
11. {
12. fileReader = new FileReader(file);
13. reader = new BufferedReader(fileReader);
14. while ((data = reader.readLine()) != null)
15. {
16. PackageLog pl = new PackageLog(data);
17. if(!pl.getPdfName().equals("")){
18. resultMap.put(pl.getPdfName(), pl);
19. }
20. }
21. } catch(Exception e){
22.
23. }
24. finally
25. {
26. try{
27. if (reader != null) reader.close();
28. if (fileReader != null) fileReader.close();
29. }catch(IOException ioe){
30.
31. }
32. }
33. return resultMap;
34. }
Almost every project that I work will have some sort of tab delimited file that I need to read in and return a lookup Map of it. I find myself rewrite the same method over and over again. I want to create something more generic so I don't have to do copy and paste code all the time. From the below code, I only change line 9, and 16-19. So I only change the Map<key, value>
, and the implementation of how I want to input data into the Map. Is there a way make a generic process from this, so every time I want to invoke this method, all I need is to provide my implementation of how I want to input data into Map, and somehow change the Map to a more generic type as well.
1. public Map<String, PackageLog> readAllLogsIntoMap(File file){
2. if (!file.exists())
3. {
4. return new HashMap <String, PackageLog> ();
5. }
6. BufferedReader reader = null;
7. FileReader fileReader = null;
8. String data = null;
9. Map <String, PackageLog> resultMap = new HashMap <String, PackageLog> ();
10. try
11. {
12. fileReader = new FileReader(file);
13. reader = new BufferedReader(fileReader);
14. while ((data = reader.readLine()) != null)
15. {
16. PackageLog pl = new PackageLog(data);
17. if(!pl.getPdfName().equals("")){
18. resultMap.put(pl.getPdfName(), pl);
19. }
20. }
21. } catch(Exception e){
22.
23. }
24. finally
25. {
26. try{
27. if (reader != null) reader.close();
28. if (fileReader != null) fileReader.close();
29. }catch(IOException ioe){
30.
31. }
32. }
33. return resultMap;
34. }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以执行以下操作。
You could do the following.
我认为你应该使用 CSV 解析器,例如 http://opencsv.sourceforge.net/
I think that you should use are CSV parser, e.g. http://opencsv.sourceforge.net/
Harry,考虑到 Java 对 D 编程语言等类型没有很好的支持,您必须重构您的方法以具有如下签名:
public MapcsvToMap(File argFile, char argSeparator, Class[]) {}
。然后,您可以像这样调用它:mymap = csvToMap("/tmp/some.log", ';', { Date.class, String.class, Double.class});
。此调用可用于解析包含诸如2011-11-23;Some Name;232.22
之类的行的 CSV 文件Harry, considering that Java does not have a nice support for type typles like, say, the D programming language, you have to refactor your method to have signature like:
public Map<String, Object[]> csvToMap(File argFile, char argSeparator, Class[]) {}
. Then, you would invoke it like:mymap = csvToMap("/tmp/some.log", ';', { Date.class, String.class, Double.class});
. This invocation may be used to parse a CSV file containing lines like2011-11-23;Some Name;232.22
例如:
以及可能的用途:
考虑到如果您想提供不同的地图实现,您只需要
newMap()
方法。您可以在泛型类中执行new HashMap()
。您可能还想定义钩子方法(可重写的,可能为空的方法)来处理异常。
For instance:
And a possible use:
Take into account you only need the
newMap()
method if you want to provide different map implementations. You could well donew HashMap<K, V>()
inside the generified class.You might also want to define hook methods (overridable, maybe-empty methods) for handling exceptions.