• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing a text file

 
ryan bohnert
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am needing help parsing a text file. I need a way to split the lines with the | .

So first word should be "1", second word should be "RIALTO".

Sample Data looks like
1|RIALTO|TOTL|1547|73|74|74|74|74|74|74|75|75|76|75
1|RIALTO|CO|222|76|76|76|76|76|76|76|76|76|76|77

Sample code

import java.io.*;

public class Test {
public static void main(String [] args){

File myFile = new File("c:/sample_aprs.txt");
BufferedReader in = new BufferedReader(new FileReader(myFile));

String strLine;

while ((strLine = in.readLine()) != null){

// Print the content on the console
System.out.println(strLine);

String[] words = strLine.split ("|");
System.out.println("Word 1 : " + words[0]);
System.out.println("Word 2 : " + words[1]);
}

//Close the input stream
in.close();

}

}
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ryan,
Please use CodeTags while posting code.
So what is it that you want to know from us? Is it that your program does not perform as expected?
If the file you are parsing can be big, you may be better of using a StreamTokenizer.
 
ryan bohnert
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking for an example of how to parse a line delimited with a "|" into variables. I have small files and large files.


Sample Data looks like this:
1|RIALTO|TOTL|1547|73|74|74|74|74|74|74|75|75|76|75
1|RIALTO|CO|222|76|76|76|76|76|76|76|76|76|76|77

 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch.
You've parsed the line into an array called "words". That's a variable. What more do you want to do?
 
ryan bohnert
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example above didn't print anything for words[0] and words[1].

I was able to come up with this to work perfectly.



Thanks Nitesh Kant, this worked great.
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ryan bohnert:
The example above didn't print anything for words[0] and words[1].


That's odd. It sure looks like it should work. Just be aware that StringTokenizer is deprecated:
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

Java API Docs
 
ryan bohnert
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats going to replace StringTokenizer?
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the text I quoted above:
the split method of String or the java.util.regex package
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan,
I suggested you to use java.io.StreamTokenizer and not StringTokenizer!

Joe,
Is the use of StreamTokenizer also discouraged?
I thought it will be a better choice because it will not load the entire string in memory at a time and run the split on the entire content which may be a bad idea if the file is huge.
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitesh Kant:
Is the use of StreamTokenizer also discouraged?


The API does not state that StreamTokenizer is discouraged, so I think your suggestion was fine.

Originally posted by Nitesh Kant:

I thought it will be a better choice. . .

Our friend was processing a line at a time, so I don't think that would be a problem (for reasonable line lengths, of course). He's using a BufferedReader which will buffer 8k of the file anyway.
I'm curious as to why String.split() was not working. Unless his line starts with unprintable characters, it should have worked.
[ February 29, 2008: Message edited by: Joe Ess ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic