Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading text file and extract the file contents

Status
Not open for further replies.

bryan33

Technical User
Jul 18, 2001
2
0
0
AU
Hi !

I hope anyone can help as i'm new to using FileReader class and StringTokenizer.

I tried to read a text file and extract the file attributes i.e. the field. There are about 100 lines of that i suppose to tokenize and extract each data in that line which are separated by deliminiter "::". I am not sure how to do that, any sample to help me out.

Many thanks ....:))
Rgds,
Bryan
 
yes it is, but I think you will have a problem with the '::' as a delimiter.

The stringTokenizer class, I believe, only accepts single char delimters.

In general you want to read the file line by line, and tokenize it as it comes in. (you could get fancy and use threads, one to read the file and another to tokenize the string).

anyone know for sure about using :: as a token? -------------------------------------------
There are no onions, only magic
-------------------------------------------
 
I could not get it to work using StringTokenizer, however I did get it to work using Regular Expressions (java.util.regex). Here is a simple example on how to perform your task...Please note that this will only work w/JAVA 1.4.X. If your project uses something different I would suggest you looking into the Jakarta-ORO API (
Code:
//Done to save space
import java.util.regex.*;

public class ParseFile
{
	public static void main(String[] args) throws Exception
	{
                Pattern pattern = Pattern.compile("::");
		
		//Creating a pattern to match breaks 
		String[] result = pattern.split("One::Two::Three::::four::Five");
		
		/*
			The output should be:
						
				One
				Two
				Three
					
				Four
				Five
		 */
		
		// Splits input with the pattern
		for (int i=0; i<result.length; i++)
		{
			System.out.println(result[i]);
		}
	}
}

Hope this help you. Also for more information on this please see the Java API.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top