Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading in a text file and parsing it in java

Posted on 2004-08-13
8
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

688 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