Path interface in Java8. You have total In Java How to get list of files and search a file providing folder path? FilenameFilter Interface Example. Join the Discussion If you liked this article, then please share it on social media.
Other Popular Articles In Java How to get list of files and search files from given folder? ByteBuffer and channels. What is an Interface in Java? Beginners Guide to Java Interface.
How to use it? Example Attached. IOException ; import java. Files ; import java. Path ; import java. The key part of this program is that while loop. Once we hit the end of the file, this will lead to a null value which will end the loop. The value returned by this method is the position starting at index 0 of the search word in our current line. If the word is not found, it will return a value of Obviously we can test to see if the value is higher than -1 meaning it found the word and print the position of that word and the line we found it on.
The line count of course is incremented each time we read a line to keep track of where in the file we are at. Now this program is very basic and would work on text files that are setup to have each line terminated by a carriage return.
The method readLine will read a line until it hits this carriage return. Another limitation to this program is that it will only find the first match of each line. It would only detect the first one at position 0 and not the second at position You could solve this limitation by putting in another loop inside the while loop once you have determined that indexOf was not -1 and would loop until indexOf equals This would be a simple addition that you could make if you were interested in not only finding out if the line had the word, but all locations of the word in the text.
Of course this search method can slow down for huge text files since we are doing a sequential process of the file. So for experimental purposes I suggest that you start with a file that has no more than a few hundred lines and then expand it when you want to do some serious processing. If the text file gets too big, you might want to look at an indexed approach. Maybe we will cover this in another entry… who knows!
Read through the in-code comments of the example above and see how this is put together. Each step has been documented so that you can put the pieces together and come up with an idea of what the program is doing.
Feel free to edit the code as you see fit and make whatever modifications you want to it. Just keep in mind this is ideal searching for text files and not exactly what you want to do for binary files.
Table of Contents. Improve Article. Save Article. Like Article. Last Updated : 21 Oct, Code Implementation Java. String initials;. MyFilenameFilter filter. Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Previous Sort and Search an Element in Java. Recommended Articles.
0コメント