Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading in a text file and parsing it in java

Posted on 2004-08-13
8
Medium Priority
?
275 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 750 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

971 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