The below code can search the values, but now I have to replace the value which is being searched .Please help I'm new to java .This should work as any text editor which has a search and replace facility .
public class TestClass {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Path to be searched");
String directory = scanner.next();
System.out.println("Enter String to be searched");
String searchString = scanner.next();
searchDirectory(directory, searchString);
}
static void searchDirectory(String directory, String searchString){
File dir = new File(directory);
if(!dir.isDirectory()){
System.err.println("PATH ENTERED is not directory");
return;
}
try{
for (File file : dir.listFiles()) {
if(!file.isDirectory()){
String fileName = file.getName().toLowerCase();
if (fileName.endsWith((".txt"))
||fileName.endsWith((".log"))) {
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int Count =0,
index = -1;
while ((line = br.readLine()) != null) {
Count++;
index = line.indexOf(searchString);
if(index != -1){
System.out.println("Text '"+searchString+"' Found at position "+index +" in file "+ fileName +" at Line No "+Count);
}
}
}
}else{
System.err.println("TestClass.searchDirectory(DIR)"+file.getAbsolutePath());
// searchDirectory(file.getAbsolutePath(), searchString);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}