>" type. Please let me know how it can be " name="description">
Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Reading Text file and storing in map

Have Programming Question? Ask it here!
 
 
 


Pragati
Reading Text file and storing in map
2 Answer(s)      3 years and 7 months ago
Posted in : Java Beginners


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 how it can be done?


Advertisement
View Answers

February 17, 2011 at 11:46 AM


Java read text files and store the value in a MAP

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());
    }
  }
}


February 17, 2011 at 2:39 PM


thanks for the answer... i am having 10 records in each file and i have 4 files. all 10 records from file 1 are inserted at first index. it behaves similarly for all. can you suggest me another option?



Related Tutorials/Questions & Answers:
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
Reading a text file in java
Reading a text file in java  What is the code for Reading a text file... in java.io.* package for reading and writing to a file in Java. To learn more about reading text file in Java see the tutorial Read File in Java. Thanks
Advertisements
reading the records from a .xlsx file and storing those records in database table
reading the records from a .xlsx file and storing those records in database... file and store that records in database table. I tried like this public class...(); } } } } Here the problem is after reading
Reading text from image file - Java Beginners
Reading text from image file  How Read text from image file
Storing and Reading data
Storing and Reading data   Hello, I'm developing a GUI application as part of an assignment but stuck on how my program stores and reads the data the user entered into the GUI table I created. I also wanted to apply a java code
Map words to line number in text file and show occurence
Map words to line number in text file and show occurence  hi i want to Map words to line number in text file and show occurrence of word in java coding
Remote file storing in mobile
Remote file storing in mobile  Hai.. i want to do my semester project..i have selected a topic "Remote file storing in mobile"..it is in the form like any Java enabled GPRS based mobile phone users can store their images,video
Remote file storing in mobile
Remote file storing in mobile  Hai.. i want to do my semester project..i have selected a topic "Remote file storing in mobile"..it is in the form like any Java enabled GPRS based mobile phone users can store their images,video
Reading big file in Java
Reading big file in Java  How to read a big text file in Java program?   Hi, Read the complete tutorial at How to read big file line by line in java? Thanks
svg file reading and display
svg file reading and display  i want a sample program for reading and displaying the content of a svg file
Stoting file name in Map
Stoting file name in Map  how to store id and file name in map
Best way to reading file in java
Best way to reading file in java  Hi, As a beginner I want to learn about Java programming and make a program for reading a text file in java. What is the best way for reading file in Java? Thanks
Reading a big file effeciently
Reading a big file effeciently  How to read a large text file quickly without memory error in Java? What is the best method to read a big file very efficiently? Thanks   Hi, Kindly check the program Java Read File
Thread for reading txt file
Thread for reading txt file  how to use 3 thread to read 3 txt file? To create three threads for reading the file and three threads for getting the strings out of the queue and printing them. thanks
External file reading in jsp
External file reading in jsp  i have written a jsp page(ReadExt.jsp) in my application which reads a text file content from a system... file. It uses BufferedReader class to read that particular file. <%@ page
External file reading in jsp
External file reading in jsp  i have written a jsp page(ReadExt.jsp) in my application which reads a text file content from a system...; The given code reads the given file. It uses BufferedReader class to read
Problem reading word file
Problem reading word file  Deepak you provide me code for extarcting equation from a word file and also to write in a word file.But when I again want to read preveously created word file(created by your code) it gives an error
Reading an excel file into array
Reading an excel file into array  Hi, I'm trying to read in an excel file, search a column for containing key words (entered by a user) and then displaying the matching rows in a table. I'm fairly new to JavaScript. Can anyone
Line by Line reading from a file using Scanner Class
In this section, you will get to know about reading line by line a text file using java.util.Scanner class
Reading file into bytearrayoutputstream
input stream and byte output stream. This is and good example of reading file... we have used the class InputStream for reading the file content into byte...*; /** * Example of Reading file into byte array and then write byte array into a file
Storing content from file path to an array
Storing content from file path to an array  Hi, I have a path... would like to access the "Animation.txt" file from this path and store the contents in this text file to an array. I am using jsp. I can access my path but how
Writing and Reading A File
and reading data from file for our assignment, but just stuck on how to proceed... into the text file. import java.io.*; import java.util.*; import javax.swing.*; import...;To extract the data from text file and save it to jtable, visit the following links
Java file in memory
Java file in memory This section illustrates you how to map an entire file into memory for reading. Description of code: We used the FileChannel class.... FileChannel: This is an abstract class which is used for reading, writing, mapping
reading a csv file from a particular row
reading a csv file from a particular row  how to read a csv file from a particular row and storing data to sql server by using jsp servlet
storing records which of a file into a table of mssql database
storing records which of a file into a table of mssql database  I have a requirement like this, A file contains some records with headers same as column names of a table. I have to store those records into a table
file reading - Swing AWT
file reading  hi everybody, i want to know how to read contents of doc or exe file except for txt file in java.   Hi Friend, Use Jakarta POI library to read the contets of doc file: Here is the code: import
Reading .doc file using swing
Reading .doc file using swing  Sir, Could U Please Tell me the Way To Read .doc file in java using swing,with code
Reading a xml file - JSP-Servlet
Reading a xml file  Thanks for ur answer sir but problem is that i have to do a reading a xml file of a employee record and then i have to use a employee details to send mail to those employees how to do i sir please help me
xml file reading using java
xml file reading using java  hi deepak I want to read some data from xml file and send that output to particular email address using java  ... n.getNodeName().equals("#text"); } public static void main(String[]args
Reading a xml file - JSP-Servlet
Reading a xml file  how to read a xml file using jsp and then i have to retrive a data from that file use it in code?  Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/parsing-xml.shtml
Reading and querying an Excel file in JavaScript
Reading and querying an Excel file in JavaScript  Hi, I'm trying to read in an excel file, search a column for containing key words (entered by a user) and then displaying the matching rows in a table. I'm fairly new
Storing records of a file inside database table in java
Storing records of a file inside database table in java  Here is my requirement, I have a file which contains some number of records like... in student.csv file).Please help me in resolving this problem. Thanks &
problem while reading .csv file
problem while reading .csv file  I have a problem here..... i am reading a .csv extention file which has some value....but the problem is der is an amount column which contains , in between (eg) 3,899.00 which inturns creates
Reading a file from Jar JAVA
Reading a file from Jar JAVA  I have added one excel sheet into a jar file. can anybody tell me how i can read that file. actually when i am running code from eclipse i able to read it but when i am adding that jar file
Reading a file from Jar JAVA
Reading a file from Jar JAVA  I have added one excel sheet into a jar file. can anybody tell me how i can read that file. actually when i am running code from eclipse i able to read it but when i am adding that jar file
Reading string from file timed
Reading string from file timed  So I want to make a file reader/ buffered reader that reads a new line of the textfile, lets say every 30 second. Like it reads the first line, waiting 30 seconds, read the next line and so one
reading and displaying svg file through java
reading and displaying svg file through java  sample program for reading a svg file
reading dropdown values from properties file in jsp
reading dropdown values from properties file in jsp  reading dropdown values from properties file in jsp
text file
text file  Hello can I modify the program below so that all the numerical data is stored in an external text file,that is the data contained in the array list.Thank you! mport java.util.*; import java.text.*; import
text file
text file  Hi can you help me I have to modify the program below so that all the data held in it is stored in an external text file.So there should... at the start of the program from a seperate external text file.Thank you! mport
Sorting text file
Sorting text file  Hello friends, i'm writing search page for my local website. i'm storing all file list in text file. And also adding a value... according to begin value. Text File having list like this: 5| 3| can anyone
initialise array by reading from file - Java Beginners
initialise array by reading from file  Hello, I wnat to know how i would initialise an array by reading a text file, which contains a simple pattern. for example the file may look as shown below, with the star character
Reading RDF file using Java code in Eclipse
Reading RDF file using Java code in Eclipse  Could you please tel me what this statement means - Model model = ModelFactory.createDefaultModel
Writing to and reading from a binary file in java.
Writing to and reading from a binary file in java.  I have written the following code to convert an ASCII text file to a binary file: public static...(); } m_dis.close(); There are 85,000 records in the text file all
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write... into a separate file that already exists in computer. map: a d2rq:ClassMap
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write... into a separate file that already exists in computer. map: a d2rq:ClassMap
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write... into a separate file that already exists in computer. map: a d2rq:ClassMap
Produces XML file but format not correct for storing data using JSP and XML
Produces XML file but format not correct for storing data using JSP... entered by user in XML file ,It stores data entered in XML file... is entered in XML file. XML file created : <?xml version = '1.0' encoding
Java error reading from file
Java error reading from file       Java error reading file... reading from file. For this we have a class error reading from file. Inside
Java example for Reading file into byte array
Java example for Reading file into byte array. You can then process the byte... in Java for reading file into byte array. Sometimes it becomes necessary... code example explains the process of reading file into byte array
Advertisements
 

 

 

DMCA.com