• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Read columns from a file ...

Hello experts,

  I have a file:

  Name, gender, age, phone, address
  A,  F. 12, 123456, ABCDE
  B, M, 13, 111111, BCDEF
  :

 What would be the best (fast way) to read the data in the file and
 then put (Name, address) in a Hashmap ?

 thanks.
0
meow00
Asked:
meow00
4 Solutions
 
Manikandan ThiagarajanSenior consultantCommented:
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rrzCommented:
If your format will always remain the same, then you could use something like the following.  

import java.io.*;
import java.util.*;
public class meow {
  public static void main(String args[]) {
     try{
         BufferedReader br = new BufferedReader(new FileReader("meow00.txt"));
         HashMap map = new HashMap();
         String line = "this will hold current line";
         line = br.readLine(); // header not put
         while((line = br.readLine()) != null){
                                      String[] tokens = line.split(",");
                                      System.out.println(tokens.length);
                                      map.put(tokens[0].trim(),tokens[4].trim());
         }
         br.close();
         System.out.println(map);
        }catch(FileNotFoundException fnfe){System.out.println("fnfe");}
         catch(IOException ioe){System.out.println("ioe");}
  }
}
0
 
rrzCommented:
Maybe you should add this line as well  
catch(ArrayIndexOutOfBoundsException e){System.out.println("problem with format");}
0
 
CEHJCommented:
:-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now