K
kavithadayalanvit
i want to search a particular word in the file using java.
can u give any specific link for this
regards
kavitha
can u give any specific link for this
regards
kavitha
i want to search a particular word in the file using java.
can u give any specific link for this
regards
kavitha
Hi kavitha,
u can try out the following code , it allows wild cards ,but is case
sensitive.Please tell me if u need that too??
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RnD {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("SAXELBY");
BufferedReader reader = null;
try {
int lineNumber=0;
reader =
new BufferedReader(
new FileReader("C:\\00000001.txt"));
while (true) {
lineNumber++;
String curline = reader.readLine();
if (curline == null) {
break;
}
Matcher matcher = pattern.matcher(curline);
while (matcher.find()) {
System.out.println("at "+lineNumber+" "+
matcher.group()
+ " start="
+ matcher.start()
+ " end= "
+ matcher.end());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
i want to search a particular word in the file using java.
can u give any specific link for this
[sNip](e-mail address removed) wrote: [sNip]while (true) {
To depend on exceptions as flow control is a bad practice.
Hi kavitha,
u can try out the following code , it allows wild cards ,but is case
sensitive.Please tell me if u need that too??
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RnD {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("SAXELBY");
BufferedReader reader = null;
try {
int lineNumber=0;
reader =
new BufferedReader(
new FileReader("C:\\00000001.txt"));
while (true) {
lineNumber++;
String curline = reader.readLine();
if (curline == null) {
break;
}
Matcher matcher = pattern.matcher(curline);
while (matcher.find()) {
System.out.println("at "+lineNumber+" "+
matcher.group()
+ " start="
+ matcher.start()
+ " end= "
+ matcher.end());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
thanks
lab
To depend on exceptions as flow control is a bad practice.
Gordon said:....
BTW exceptions *are* a form of flow control, whether you like them or
not.
thanks for ur reply. it will also be helpful if u sent the coding
which u have asked me .
Randolf Richardson said:1. The ability to share code between methods within a class,
which all end with the same functionality (this could be more
efficient than calling another method; javac would need to generate
errors such as attempts to access variables that belong to different
methods, return type mismatches, etc.)
I totally disagree. Assemblers need "goto" but no decent HLL does. ThatRandolf said:This typically occurs because the reserved word "goto" isn't
implemented. There are situations where "goto" would be very useful,
such as:
I don't use this construction in C either and, again, don't need it.0. An alternative to "break label" since label is currently
limited in where it can be located (such code could be easier to read)
Encapsulating the code in a private method makes for more readable code1. The ability to share code between methods within a class,
which all end with the same functionality (this could be more efficient
than calling another method; javac would need to generate errors such as
attempts to access variables that belong to different methods, return
type mismatches, etc.)
Yes - but that's a result of classes that throw exceptions when theirI do agree with your view that infinite loops that depend on
exceptions are a bad practice. Using conditionals to trigger a "break"
would also be better handled by making that the focus of the loop -- and
if they need to compare afterwards, then "do { ... } while (condition);"
can certainly solve that problem.
Just a suggestion designed to help you.
Randolf said:[sNip](e-mail address removed) wrote: [sNip]while (true) {
To depend on exceptions as flow control is a bad practice.
This typically occurs because the reserved word "goto" isn't
implemented. There are situations where "goto" would be very useful,
such as:
0. An alternative to "break label" since label is currently
limited in where it can be located (such code could be easier to read)
1. The ability to share code between methods within a class,
which all end with the same functionality (this could be more efficient
than calling another method; javac would need to generate errors such as
attempts to access variables that belong to different methods, return
type mismatches, etc.)
Randolf Richardson wrote:
Encapsulating the code in a private method makes for more readable code
as well as eliminating code duplication.
Martin said:This is because try...catch blocks that include class
instance declarations screw up otherwise clean block structures by
preventing me from grouping all object instance declarations at the
start of a block rather than sprinkling them through the code.
Alex said:I disagree, I think goto was dumped for very good reasons, and
structured programming (i.e. no gotos; use methods and refactoring
techniques instead) makes a lot more sense.
Completely agree, Martin. "Goto" is quite nonsense - it's not needed
from a theoretical as well as from a practical point of view.
Plus, with modern JVM's the private method is typically inlined *if* it
is a performance bottleneck. If not, it's not worth bothering about the
method invocation overhead anyway.
Side note: I am amazed that the "issue" of "goto" keeps popping up. The
high time of Basic has long since gone but people still seem to cling to
this ancient concept. Or do all these folks use assembler during their
daily work?
Yes, I know your preference is normal and widely used.Lew said:Personally I prefer to declare variables closest to the point of use,
rather than all at the beginning of a block.
This discussion is very interesting. One of the reasons I think
"goto" keeps coming up is that there is demand for it.
I'm not convinced that "goto" is a bad thing, especially given the
numerous "while (true) { ... }" loops that I've seen over the years
(granted, those are usually examples of bad coding style, but many
people use it because they don't have "goto" at their disposal).
To me, the limit on the placement of labels at the beginning of a
loop, followed by a partial "goto" using the "break label;" instruction
is actually more difficult to follow because first one has to find the
label in preceeding code, then search for the end of the loop to follow
the program flow from there. If the label could at least be placed
anywhere from the beginning of the loop to immediately after its end,
with "goto" enabled for this, I do think this could actually make things
a bit better.
I definitely don't agree that removing "goto" eliminates "spaghetti
code" and works against the OO concept (as someone pointed out in a
separate reply), rather it's the individual developers who will continue
to write terrible code even without "goto" (which seems to happen now).
The masses seem to be convinced that "goto" is bad, but in the
history of the world there are countless examples where the masses were
wrong (e.g., political elections, setting women on fire to determine if
they were witches, removing tonsils because medical experts at the time
believed them to be useless, mis-treatment of leppers, etc.).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.