Solved

Reading in a text file and parsing it in java

Posted on 2004-08-13
8
249 Views
Last Modified: 2010-03-31

Hi

I have a large text file that i want to read into java and parse the information in it. Can anyone tell me the best way about doing this.

example of one line of the file (each line is the same format)

there are 3 lines above this that are completly irrelevant that i wont need either

9406458572012631790140390464112    00000003500  textNotNeededhere          textnotneededhere   Joe Bloggs

I need to parse the numbers at the front into a database, as in the first 6 are a value, the next 8 are a value and so forth

Any help would be great!!!
Thanks,
Suzy
0
Comment
Question by:fyness
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11791698
Use a StreamTokenizer. Here's an example - you need to


http://javaalmanac.com/egs/java.io/ParseJava.html

use the number bit
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11791707
You need only this bit:

case StreamTokenizer.TT_NUMBER:
               
0
 
LVL 7

Accepted Solution

by:
tomboshell earned 250 total points
ID: 11791761
use BufferedReader, FileReader, StringTokenizer

like:

BufferedReader br = new BufferedReader(new FileReader(yourTextFile));
String line=null;
int cntr=0;
while((line = br.readline())!=null){
  cntr++;
  if(cntr<4) continue;  // skip the first three
  String[] items = line.split(Character.toString('\t'));
  for(int i = 0; i < items.length; i++){
    // now you have an array with each element in the array being one of the entries
    // this loop will go through the items, simply store the items based upon the value of 'i' with '0' being the first position
    // I don't know how you are storing the items so will leave that up to you...
     //
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 11791865
You'll find that the StreamTokenizer will give you better performance ;-)
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 11791970
I have wondered about it.  Everytime I read the javadocs about StreamTokenizer I get the serious impression that it is ideally suited to parse items like code source files and not really what I parse.  But ya, I can see where it could be used. Would also have to notice the line numbers to be able to skip the first three, and it provides the lineNo() method.  Then watch the tokens and positions since it looks like most of the numbers on the lines are taken for storage, and all the text except the name is ignored.  I would think that would result in a bit more questioning of the the values contained.  But then if it had something like 'tokenNumber' property or method then this would be no problem.  But I am willing to learn :)

Could also use a StringTokenizer to parse the individual lines, but I kinda like the split method.  That way I work with arrays which make it easy to think of the data placed into table structures.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11791992
>>Everytime I read the javadocs about StreamTokenizer ...

Yes, i agree. The reason i recommended it in this case is that you can handily ignore everything except numbers

0
 

Author Comment

by:fyness
ID: 11792751
Just one other thing on the code above, at the moment the array takes in each line of the file, how could i now split up the lines ie take each array box and parse them?

Thanks
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 11793057
It reads each line, line per line in a loop until the end-of-file is reached.  Each loop iteration breaks the line into an array with each array element being one of the elements of that line.  I was assuming that you were working with tab-separated files.  
I would then assume that you were providing some easy way to set the values...like

setColumnOne(items[0]);
setColumnTwo(items[1]);
// items 2 & 3 being not used.
setUser(items[4]);  


That way you can perform any special handling on the individual values as needed.  

Have a great weekend!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to configure empty element in XML Document parser? 15 44
jmeter usage 4 34
Netbeans and org.apache.commons.lang3 issue 3 22
Html split(text) 2 27
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question