The Artima Developer Community
Sponsored Link

Java Answers Forum
how to search a word in a file

5 replies on 1 page. Most recent reply: Aug 15, 2003 7:09 AM by Greg Lehane

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
aj3423 aj

Posts: 16
Nickname: aj3423
Registered: Aug, 2003

how to search a word in a file Posted: Aug 13, 2003 8:11 AM
Reply to this message Reply
Advertisement
how can i get a charactor in a file one by one and compare it with "a" ?(not use readLine)


Greg Lehane

Posts: 33
Nickname: glehane
Registered: Jun, 2003

Re: how to search a word in a file Posted: Aug 13, 2003 11:36 AM
Reply to this message Reply
The read() method in BuffereredReader reads a character at a time. It returns -1 when at the end of the stream/file.

=- Greg

aj3423 aj

Posts: 16
Nickname: aj3423
Registered: Aug, 2003

Re: how to search a word in a file Posted: Aug 13, 2003 5:40 PM
Reply to this message Reply
all read() methods return "int", eg. character "A" will be turned to 65(its ascII),then how can i compare it with the charecter "a"?

Greg Lehane

Posts: 33
Nickname: glehane
Registered: Jun, 2003

Re: how to search a word in a file Posted: Aug 13, 2003 7:29 PM
Reply to this message Reply
try using 'A' instead of "A"

=- Greg

aj3423 aj

Posts: 16
Nickname: aj3423
Registered: Aug, 2003

Re: how to search a word in a file Posted: Aug 15, 2003 1:56 AM
Reply to this message Reply
then how can i check if a String "abcde" exists in a file ?

Greg Lehane

Posts: 33
Nickname: glehane
Registered: Jun, 2003

Re: how to search a word in a file Posted: Aug 15, 2003 7:09 AM
Reply to this message Reply
Well that's different then isn't it. If you are using the jdk 1.4.x then you can use the rather powerful regular expression search package it comes with. Otherwise there is a package in the apache jakarta project.

Or you could write your own search mechanism. You would have to keep a buffer as you read the file. Be imaginative, look at the methods available to you in class String etc.

=- Greg

Flat View: This topic has 5 replies on 1 page
Topic: Any similar technique like C's #ifdef in Java? Previous Topic   Next Topic Topic: What version are the JNI header files suppose to work with?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use