Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6783439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:51:50+00:00 2026-05-26T16:51:50+00:00

Almost every project that I work will have some sort of tab delimited file

  • 0

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. }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:51:51+00:00Added an answer on May 26, 2026 at 4:51 pm
    1. Put this method in an abstract Util class. Use generics.
    2. Delegate on abstract methods the logic to perform on lines 9 and 16-19.
    3. Define these abstract methods in the uses of this class

    For instance:

    public abstract class ReaderUtil<K, V> {
    
     protected abstract Map<K, V> newMap(); 
     protected abstract void doThings(String data, Map<K, V> resultMap);
    
     public Map<K, V> readAllLogsIntoMap(File file){
          if (!file.exists()){
              return newMap();
          }
          BufferedReader reader = null;
          FileReader fileReader = null;
          String data = null;
          Map <K, V> resultMap = newMap();
          try {
             fileReader = new FileReader(file);
             reader = new BufferedReader(fileReader);
             while ((data = reader.readLine()) != null){
                 doThings(data, resultMap);
             }
         } catch(Exception e){
    
         }
         finally{
             try{
                 if (reader != null) reader.close();
                 if (fileReader != null) fileReader.close();
             } catch(IOException ioe){
    
             }
         }
         return resultMap;
     }
    }
    

    And a possible use:

    ReaderUtil<String, PackageLog> reader = new ReaderUtil<String, PackageLog>(){
        protected Map<String, PackageLog> newMap() { 
            return new HashMap<String, PackageLog>(); 
        }
        protected void doThings(String data, Map<String, PackageLog> resultmap){
            PackageLog pl = new PackageLog(data);
            if(!pl.getPdfName().equals("")){
                resultMap.put(pl.getPdfName(), pl);
            }
        }
    };
    Map<String, PackageLog> myMap = reader.readAllLogsIntoMap();
    

    Take into account you only need the newMap() method if you want to provide different map implementations. You could well do new HashMap<K, V>() inside the generified class.

    You might also want to define hook methods (overridable, maybe-empty methods) for handling exceptions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Almost every ios app now has something like Feed option. Programming that usually includes
well for some strange reason IE gives me and InvalidAuthenticityToken error almost every time
I've read about postion:absolute problems and tried almost every possible solution. Including positioning divs
Almost all of the examples in the jQuery tutorials that I've read, usually use
In the project I work on, we handle Medical Billing. Every time the state
I'm trying to make a clean maven setup on some legacy project that uses
Duplicate post, see: When do you use the "this" keyword? On almost every project
In my current django project I have a model that stores very long strings
I have some problems importing an existing C++ project into my XCode. I already
I have a project that utilizes Entity Framework. I want to run SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.