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
Win a copy of
TDD for a Shopping Website LiveProject
this week in the
Testing
forum!
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
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
I/O and Streams
how to search for string in file
padma patil
Ranch Hand
Posts: 41
posted 20 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 20 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
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
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...