Java
Jeremy Turner
jeremy at linuxwebguy.com
Sat May 1 22:13:15 CDT 2004
On Fri, 2004-04-30 at 15:34, Ben Coffman wrote:
> I have a file I want to locate a word within the file and then erase that
> word without erasing the rest of the file. So far all I can get out of this
> is to read in each character until I find the characters in the order I need
> and then I know the location of the word and where I need to delete.
Nah... that sound's too much like C/C++ thinking. Java deals more with
strings and tokens, rather than character arrays with null terminators.
Try something like this:
/*********/
import java.io.*;
import java.util.*;
public class delWord {
public static void main (String av[]) {
try {
BufferedReader is = new BufferedReader(
new InputStreamReader(System.in));
String inputLine;
StringBuffer outputLine;
String wordToDelete = "and";
StringTokenizer words;
String curWord;
while ((inputLine = is.readLine()) != null) {
words = new StringTokenizer(inputLine);
while (words.hasMoreTokens()) {
curWord = st.nextToken();
if (!curWord.equals(wordToDelete)) {
System.out.print(curWord);
}
}
}
is.close();
} catch (IOException e) {
System.out.println("IOException: " + e);
}
}
}
/********/
Run this with:
$ java delWord < inputFile > outputFile
Something like that would work. I'm not a certified Java hacker, but I
did stay a a Holiday Inn Express last night. :)
Jeremy
--
Jeremy Turner <jeremy at linuxwebguy.com>
http://linuxwebguy.com
More information about the Kclug
mailing list