swan
Posts: 7
Nickname: swan
Registered: Aug, 2003
|
|
Re: i want to check if a string exists in a file
|
Posted: Sep 2, 2003 12:59 AM
|
|
Hi there,
Here is your code, modified, and the place where the mistake was committed.
import java.io.* ;
public class Test { public static void main(String args[]) throws IOException { boolean flag = true; while(flag) { flag = false; prt("imput the filename : "); File Fname = new File(ImputString()); if (Fname.exists()) { prt("imput the word to search : "); String theWord = ImputString(); int length = theWord.length(); Check(Fname,theWord, length); // earlier it was ImputString() instead of theWord prt("the word not found ."); //Fname.close(); ,is this right ? System.exit(0); } else { System.out.println("file not found : " + Fname); prt("try again ?(y/n) : "); BufferedReader YorN = new BufferedReader(new InputStreamReader(System.in)); String Choice = YorN.readLine(); if (Choice.toLowerCase().equals("y")) { //check "y" or "n" flag=true; } else { System.exit(0); } } } }
static String ImputString() throws IOException { String str = null; BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); str = read.readLine(); return str; }
static void Check(File theFile, String word, int length) throws IOException { String theLine = null; int i = 0; int x = 0; int m = 0; int p = 0;
char Array[] = word.toCharArray();
FileInputStream fis = new FileInputStream(theFile); DataInputStream in = new DataInputStream(new BufferedInputStream(fis)); while(in.available() != 0) { while((char)in.read() == Array[p]) { in.skip(1); p++; if(p == length) { prt("found ."); fis.close(); System.exit(0); } } } }
static void prt(String s) { System.out.print(s); }
}
|
|