File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Parsing a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Parsing a text file" Watch "Parsing a text file" New topic
Author

Parsing a text file

ryan bohnert
Greenhorn

Joined: Feb 27, 2008
Posts: 19
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

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
ryan bohnert
Greenhorn

Joined: Feb 27, 2008
Posts: 19
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

Joined: Oct 29, 2001
Posts: 8964
    
    9

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?


[How To Ask Questions On JavaRanch]
ryan bohnert
Greenhorn

Joined: Feb 27, 2008
Posts: 19
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

Joined: Oct 29, 2001
Posts: 8964
    
    9

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

Joined: Feb 27, 2008
Posts: 19
Whats going to replace StringTokenizer?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

From the text I quoted above:
the split method of String or the java.util.regex package
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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

Joined: Oct 29, 2001
Posts: 8964
    
    9

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing a text file