Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within I/O and Streams
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Ron McLeod
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
I/O and Streams
how to search for string in file
padma patil
Ranch Hand
Posts: 41
posted 23 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I am new to
java
file system.
Can anybody tell me how I can search a file to find a perticular
string
in it.
Do I need to fetch each string/word and check for the matching string?
Please help me out
thanks
padmashree
Siva Prasad
Ranch Hand
Posts: 104
posted 23 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Ok here is the code I wrote for this.
this takes two arguments
1 - string to find
2 - filename
import java.io.*; class FindString { public static void main(String[] args) throws IOException { if(args.length != 2) { System.out.println("Usage: FindString <string> <filename>"); System.exit(1); } RandomAccessFile in = new RandomAccessFile(args[1],"r"); String s = null; int i, loc = 0; for(i=1;(s = in.readLine()) != null;i++) { loc = s.indexOf(args[0]); if (loc == -1) continue; else { System.out.println(args[0]+" found in line - "+i); System.out.println(s); } } System.out.println(args[0]+" found "+--i+"no of times in the file "+args[1]); } }
You can have a look at indexOf methods from String class.
HtH
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How to search a file???
I have a question on User Interface!
How to write regular expression to skip a specific charaters in word
Searching a File
Searching for a string in a file
More...