Solved

Reading in a text file and parsing it in java

Posted on 2004-08-13
8
236 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 55
How to execute a Python program and gather return output in Java 2 31
Desingning Refactoring existing code 2 24
ejb on wildfly 5 20
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

803 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