Parsing Text Files in Java

July 14, 2008 at 3:08 pm 18 comments

The following code is designed to parse (comma, tab, etc.) delimited files in Java.

private static ArrayList parseDelimitedFile(String filePath, String delimiter) throws Exception
{
  ArrayList rows = new ArrayList();

  FileReader fr = new FileReader(filePath);
  BufferedReader br = new BufferedReader(fr);

  String currentRecord;
  while((currentRecord = br.readLine()) != null)
    rows.add(currentRecord.split(delimiter));

  br.close();

  return rows;
}

Advertisements

Entry filed under: CodeSnippet, Java.

Peeking at Large Files in Python

18 Comments Add your own

  • 1. jk  |  July 28, 2008 at 3:54 am

    Will this help to parse a huge text file ( about 500 Mb ).

    Please help me on this. Actually I need to parse from a big file to a table. Also I need to do some data manipulation while parsing the text files lines fileds with exisiting table.

    What would be the fastest way to do this.

    Thanks

  • 2. utah_guy  |  July 28, 2008 at 2:34 pm

    This should work to parse large files. I have used similar code to process multi-GB files. What you’d have to change, though, is that this code stores the results in an ArrayList object, so if you are processing huge files it will consume a lot of memory. But if you are pulling the data from a file and then storing it in a database immedidately, you should not have to worry about memory. If you’re wanting some code samples on how to manipulate the text after pulling from the file and before inserting into the database, please send me more details, and I’ll see what I can cook up.

  • 3. Diklla  |  December 7, 2009 at 10:42 am

    Thanks! this really helped me out 🙂

  • 4. stenix  |  December 18, 2009 at 9:12 am

    If you want more flexibility and validation functionality you could consider using the open source library http://jsapar.tigris.org. It uses an xml based schema to describe the structure of the file to parse.
    You can register an event listener for each line that is parsed, thus it does not require to load more than one line at a time into the memory.

  • 5. revathi  |  September 29, 2011 at 6:52 am

    Hi,

    I am having one data file which contains the below format

    [HEADER: BUSINESS DATE=20110914]

    [RESTDAYS]
    AED=FRI
    AFN=FRI:SAT

    II want to get the currency asndays alone from the above file and store it to arraylist.. Please help me out…

  • 6. revathi  |  September 29, 2011 at 6:56 am

    Hi,

    I am having one data file which contains the below format

    [HEADER: BUSINESS DATE=20110914]

    [RESTDAYS]
    AED=FRI
    AFN=FRI:SAT

    II want to get the currency asndays alone from the above file and store it to arraylist.. Please help me out…

  • 7. revathi  |  October 3, 2011 at 8:34 am

    rows.add(currentRecord.split(delimiter));

    This will give an error if string array ( ABC:ACD)
    exists

  • 8. revathi  |  October 3, 2011 at 8:35 am

    import java.io.*;
    import java.util.*;
    public class ParsingFileProp {

    Properties p=new Properties();

    public static void parseDelimitedFile( String filePath1, String outPath1) throws Exception
    {
    FileWriter f=new FileWriter(outPath1);
    BufferedReader br = new BufferedReader(new FileReader(filePath1));
    String currentRecord=null;
    List records =new ArrayList();
    while((currentRecord = br.readLine()) != null)
    {
    records.add(currentRecord);
    }
    Iterator itr = records.iterator();
    while(itr.hasNext())
    {
    String element = (String) itr.next();
    if(element.startsWith(“[“))
    continue;
    else if(element.contains(“=”))
    {
    String result = java.util.Arrays.toString(element.split(“\\=”));
    String currency=result.substring(1,4);
    f.append(“CURRENCY ———->”+currency+”\n”);
    int len = result.length();
    int lastindex=len-1;
    String day = result.substring(6,lastindex);
    f.append(“DAYS————–>”+day+”\n”);
    }
    f.flush();
    }
    }

    public static void main(String args[])throws Exception
    {
    Properties p=new Properties();
    try
    {
    p.load(new FileInputStream(“file1.properties”));
    }
    catch(FileNotFoundException f)
    {
    System.out.println(“DataFile is not found in properties file”+f);
    }
    String datFile = p.getProperty(“filePath”);
    String outFile = p.getProperty(“outPath”);
    ParsingFileProp p1=new ParsingFileProp();
    p1.parseDelimitedFile(datFile,outFile);

    }

  • 9. Steve  |  October 3, 2011 at 11:46 am

    What error do you get?

  • 10. revathi  |  October 13, 2011 at 12:46 pm

    Type safety: The method add(Object) belongs to the raw type ArrayList. References to generic
    type ArrayList should be parameterized

  • 11. revathi  |  October 13, 2011 at 12:53 pm

    The above error will be fixed. if we parameterize the arraylist as string type like ArrayList list = new ArrayList ();

    Though I got the error

    ” The method add(String) in the type ArrayList is not applicable for the arguments (String[]) “

  • 12. revathi  |  October 13, 2011 at 12:54 pm

    The above error will be fixed. if we parameterize the arraylist as string type like ArrayList list = new ArrayList ();

    Though I got the error

    ” The method add(String) in the type ArrayList is not applicable for the arguments (String[]) “

  • 13. bobby  |  August 10, 2012 at 11:38 am

    plz help me,i need “Read the file seperated by comma and following requirement
    1)considerble words are alphabet or combination of alphabet and numerics or nuemerics
    ex:rama,
    hari234
    raja-453.432
    rahim-872 etc

  • 14. zastaw nieruchomości  |  November 28, 2012 at 9:50 am

    Aw, this was a very nice post. Taking the time
    and actual effort to generate a very good article… but what can I
    say… I hesitate a whole lot and don’t seem to get nearly anything done.

  • 15. home security systems  |  June 6, 2013 at 12:24 pm

    Hi there, just became alert to your blog through Google, and found that it’s really informative. I am going to watch out for brussels. I’ll appreciate if
    you continue this in future. Numerous people will be benefited from
    your writing. Cheers!

  • 16. comptable paris  |  June 20, 2013 at 5:30 pm

    Opportunities- These are environmental factors that can be exploited.
    The competitor generates first party information directly.
    Owning business premises in a SMSF can make a great deal of sense for SMSF’s and business owners alike.

  • 17. Parsing Text Files in Java | Ragnarok Connection  |  July 16, 2014 at 3:55 am

    […]   Related Article: Code Comments […]

  • 18. dorothybonner36056  |  April 8, 2016 at 11:31 pm

    Terima kasih, semoga bermanfaat buat keluarga dan sahabat saya Click http://d2.ae/hool090730

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed



%d bloggers like this: