954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

# of lines in a text file

Hello,

I was wondering if there was a way to find how many lines there are in a text file, and if so can you find out how many lines in the text file start with a particular word (an identifier).

Thanks

Dark_Omen
Posting Pro
573 posts since Apr 2004
Reputation Points: 23
Solved Threads: 6
 

>I was wondering if there was a way to find how many lines there are in a text file...

http://forum.java.sun.com/thread.jspa?threadID=474733&messageID=2200710

However, speed might not be an issue here.

>so can you find out how many lines in the text file start with a particular word (an identifier).

class indexTest
{
	public static void main (String args[])
	{
	String str = "This string will be searched";
	String find = "will";

	System.out.println (str.indexOf ('s'));  // find char, no offset
	System.out.println (str.lastIndexOf ('s', 4)); // find s, offset by 9

	System.out.println (str.indexOf (find)); // find the string find, no offset
	System.out.println (str.lastIndexOf(find)); // find the string find in str, no offset
	}
}


For more advanced work with strings check out the Stringbuffer api.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Thanks a lot!

Dark_Omen
Posting Pro
573 posts since Apr 2004
Reputation Points: 23
Solved Threads: 6
 

String.startsWith(str)

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You