Nuebee August 12, pm. Very good and helpful tips…. Thanks ….. Mukesh December 29, am. Its very useful. Thank you very much. Hello, how to use regular expressions to search for an uninitialized word, for example I want to program a function that reads the text of a json file and then searches for a term i and at the end give the number of lines that contain that term.
Your email address will not be published. I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program. 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. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? 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.
Those baddies will have to be handled another day. After all, the super criminals are not easily caught! So the next time you are in a dark room at night and no where else to turn for learning file handling in Java, have no fear, DIC and Martyr2 is here! Thank you for reading! Desktop Programming File Handling Java. He has been a programmer for over 23 years.
0コメント