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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StreamTokenizer

Status
Not open for further replies.

sandeepmur

Programmer
Dec 14, 2003
295
PT
Hi,

I have a text file with 2 lines:
client=24
clientname=dragon

I am trying to use a stream tokenizer to open and read the file but having some problems. my code is as follows:

StreamTokenizer tokenizer = new StreamTokenizer(new FileReader("res.txt"));
int nextTok = tokenizer.nextToken();
while (StreamTokenizer.TT_EOF != nextTok) {
if (StreamTokenizer.TT_NUMBER == tokenizer.ttype) {
out.println("It's a number!");
out.print (tokenizer.nval + " ");
}
else if (StreamTokenizer.TT_WORD == tokenizer.ttype) {
out.print (tokenizer.sval + " ");
}
}

Its taking an awfully long time to read the file. Is there a better way to read the text file?

I need the values 24 and dragon in 2 different strings.

appreciate any help..
thnx,
sg
 
Code:
	  	Properties aaa= new Properties();
	  	InputStream aaaStream = new FileInputStream("myfile.txt");
	  	aaa.load(aaaStream);
		String client= aaa.getProperty("client");
	    String clientname= aaa.getProperty("clientname");

Or you could use a BufferedReader and call the readLine() method, and then split the String using a StringTokenizer.
 
Hi, another small help. I am obtaining the path of the current directory as in:
String original = application.getRealPath(request.getServletPath());

which is returning "C:\Program Files\Apache Group\Tomcat 4.1\webapps\site\client1\file.jsp"

My objective is to obtain the name of the present directory which is "client1". any ideas?

thnx in adv
 
I'm sure there is a better way to do it, but as a simple hack :

Code:
StringTokenizer st = new StringTokenizer("C:\Program Files\Apache Group\Tomcat 4.1\webapps\site\client1\file.jsp", "\\");

int cnt = st.countTokens();
for (int i = 0; i < cnt -2; i++) {
   st.nextToken();
}

String currentDir = st.nextToken();
System.out.println(currentDir);
 
You could also do something like this:

Code:
File curDir = new File( original );
String parentDir = curDir.getName();

The
Code:
File.getName()
method returns the last directory/file name in a path.
 
File curDir = new File( original );
String parentDir = curDir.getName();

returns only the file name while all i want is the directory name.

thnx
 
Sorry, I didn't understand your question at first. Perhaps this will work:

Code:
File curDir = new File( original );
String parentPath = curDir.getParent();
File parentDir = new File( parentPath );
String parentDirName = parentDir.getName();

Seems like more code than necessary, though.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top