Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners read from file and store using hash map
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


mahindar
read from file and store using hash map
1 Answer(s)      2 years and 2 months ago
Posted in : Java Beginners


I was stuck with a java project where I have to read a file with two different concepts and store them differently in hashmap. My data file would be something like

Adults:

Name, xyz (Key, value)
ID, 123 (key, value)

Children:

Name, abc
ID, 321 and so on........

Here two concepts adults and children in file should be stored in two different maps. Any help will be appreciated. Thanks in advance.


Advertisement
View Answers

July 25, 2012 at 12:30 PM


Here is a simple example of storing the text file data into HashMap.

import java.io.*;
import java.util.*;
class OnlyExt implements FilenameFilter{
  String ext;
  public OnlyExt(String ext){
  this.ext="." + ext;
  }
  public boolean accept(File dir,String name){
  return name.endsWith(ext);
  }
}
class MapExample{
    public static void main(String[] args)throws Exception{
        FilenameFilter ff = new OnlyExt("txt");
        File folder = new File("c:/");
        File[] files = folder.listFiles(ff);
        Map<String,String> map=new LinkedHashMap<String,String>();
        for(int i=0;i<files.length;i++){
        FileReader fr = new FileReader(files[i].getPath());
            BufferedReader reader = new BufferedReader(fr);
            String st = "",str=" ";
            while((st = reader.readLine()) != null) {
        str+=st;
        }
        map.put(files[i].getName(),str);
        }
        Set set = map.entrySet();
        Iterator i = set.iterator();
        while(i.hasNext()){
        Map.Entry me = (Map.Entry)i.next();
        System.out.println(me.getKey() + " \t:\t " + me.getValue());
    }
  }
}


Related Tutorials/Questions & Answers:
read from file and store using hash map
read from file and store using hash map  I was stuck with a java project where I have to read a file with two different concepts and store them differently in hashmap. My data file would be something like Adults: Name, xyz
Using Arraylist or Hash Map - Java Interview Questions
Using Arraylist or Hash Map  what could b the pros and cons changing between Hash Map to Array List
Advertisements
Hash Map
Hash Map  how to add the hash map objects in the list and iterate the objects
hash map
hash map  write a program in hash map , that shows the value in asending order. but not key value. ex: key value ==== ===== k5... ......... and value entry from keyboard
hash map
hash map  how to add object of test class in a hashmap test class...; } values of instance variable is coming from different database tables;  ...=2; HashMap<Integer,Test> map=new HashMap<Integer,Test>
Writing and reading from/to a serialized file through Hash Table in Java
illustrates you how to read and write from/to a serialized file through the hash table... Writing and reading from/to a serialized file through Hash Table in Java... to the specified serialized file if it exists otherwise read all contents by de
Store ResultSt Data in a Map
Store ResultSt Data in a Map  i want to store data in a hash Map .Please solve my problem as soon as possible
Read data from excel file and update database using jsp
Read data from excel file and update database using jsp  read data from excel file and update database using jsp Hi, I am using a MySQL database... upload excel file and update database using JSP ? Thanks in Advance
read text file and store the data in mysql - JDBC
read text file and store the data in mysql  when we store the data in mysql table from text file its store the data from new line to new column. how to store the data in different column from a single line of text file
java code to compare two hash map objects
java code to compare two hash map objects  java code to compare two hash map objects
Read from file java
Read from file java  How to Read from file java? What is the best method for a text file having a size of 10GB. Since i have to process the file one line at a time so tell me the very best method. Thank you
Read Specific Line from file Using Java
Read Specific Line from file Using Java Here we are going to read a specific line from the text file. For this we have created a for loop to read lines 1 to 10 from the text file. If the loop reached fifth line, the br.readLine() method
read and write a file using javascript
read and write a file using javascript  How to read and write a file using javascript
Read Property file From Jmeter
Read Property file From Jmeter  Hi, I am running web driver script from Jmeter, but while reading property file I am getting "File Not Find Exception". Please tell me how to read ".properties" file from Jmeter
Read Lines from text file
Read Lines from text file  Here's a brief desc of what my Java code does .. I'm using BufferedReader to read lines from a text files and split each... read from the text file and displays the output as desired. Unable to read the rest
Java read lines from file
Java read lines from file  Any code example related to Java read lines from file? In my project there is requirement of reading the file line by line... to read the whole file in one go. So, in my case reading file line by line is only
need to update key , value pair in map from properties file
need to update key , value pair in map from properties file  I have a map with key , value pair. with key as integer and value as string datatype. I want the keys and values to be dynamically updated into map. that is i have
how to read data from excel file through browse and insert into oracle database using jsp or oracle???
how to read data from excel file through browse and insert into oracle database using jsp or oracle???  sir.. i have number of excel sheets which...://www.roseindia.net/answers/viewqa/JSP-Servlet/28123-write-excel-file-into-the-oracle
Stoting file name in Map
Stoting file name in Map  how to store id and file name in map
Read Text file from Javascript - JSP-Servlet
Read Text file from Javascript  plz send the code How to Retrieve the data from .txt file thru Javascript? And how to find the perticular words in that file
read excel file from Java - Java Beginners
read excel file from Java  How we read excel file data with the help of java?  Hi friend, For read more information on Java POI visit to : http://www.roseindia.net/java/poi/ Thanks
how to read and write an xml file using java
how to read and write an xml file using java  Hi Can anyone help me how to read and write an xml file which has CData using java
how to get data from list to map with out using a loop?
how to get data from list to map with out using a loop?  List list...("15"); how to get this data into map with out using a loop. i want this data like this--map.put(name,id); Is it possible to do without using loop
read from cmd syntax and open the file
read from cmd syntax and open the file  i have to read the name of the database e.g., "database.txt" file from a cmd syntax and open it: java FindKeys database=db.txt;minjoinrows=integer|all how can i do
read from cmd syntax and open the file
read from cmd syntax and open the file  i have to read the name of the database e.g., "database.txt" file from a cmd syntax and open it: java FindKeys database=db.txt;minjoinrows=integer|all how can i do
How I Upload File and Store that file name in Database using JSP
How I Upload File and Store that file name in Database using JSP  Hi All, I m the beginner in JSP and I want to upload the file and store that file... this file). I refer the Employee Profile but it not work in my localhost server
how to read the .proprties file from struts - Struts
how to read the .proprties file from struts   errpr is :file not found exception:applicationresource.proprties file {system canot find file path"; How to set the file path.  Hi Friend, It seems that you haven't
To read & write a excel file using the core java
To read & write a excel file using the core java  Hai, I'm new to JavaProgram.But now i need java program to read & write a excel file so, can anyone help me to learn the above mentioned topic(link for the portion
How to read a line from a doc file
How to read a line from a doc file  How to read a line from a doc file   Hi Friend, To run the following code, you need to download POI... void main(String[] args) { File file = null; WordExtractor extractor = null
How to Read a file line by line using BufferedReader?
How to Read a file line by line using BufferedReader?  Hello Java... to Read a file line by line using BufferedReader, efficiently and using less memory... problem is to find the best way to read the file in Java. I just searched the google
Reading Text file and storing in map
Reading Text file and storing in map  Hi I have multiple text files. I want to read thoses files and store those records in map. Map will be of "LinkedHashMap<String, Map<String, String>>" type. Please let me know
Advertisements
 

 

 

DMCA.com