How to search a word in file in java -
i trying search string in file in java , what, tried . in below program getting output no data found , sure file has word searching
import java.io.*; import java.util.scanner; public class readfile { static string[] list; static string sear = "create"; public void search() { scanner scannedfile = new scanner("file.txt"); while (scannedfile.hasnext()) { string search = scannedfile.next(); system.out.println("search content:"+search); if (search.equalsignorecase(sear)) { system.out.println("found: " +search); } else { system.out.println("no data found."); } } } public static void main (string [] args) throws ioexception { readfile read = new readfile(); read.search(); } }
try :
import java.io.*; import java.util.arraylist; import java.util.list; public class readfile { static string[] list; static string sear = "create"; public void search() throws ioexception { list<string> savealllinesforrewriting = new arraylist<string>(); // need read file line line bufferedreader bufferedreader = new bufferedreader(new filereader("file.txt")); string saveline; while ((saveline = bufferedreader.readline()) != null) { savealllinesforrewriting.add(saveline); } bufferedreader.close(); // check if word exists if (savealllinesforrewriting.tostring().contains(sear)) { system.out.println("found: " + sear); } else { system.out.println("no data found."); } } public static void main(string[] args) throws ioexception { readfile read = new readfile(); read.search(); } }
Comments
Post a Comment