• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to search a file???

 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I want to search for a particular string in a file.

How can i achieve that??
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some optimizations to the process, but at the most simple, read the file until you find the string.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i am facing a problem with this search thing. The problem is i am storing the search string in a variable and using it to search an *.sql file. But the problem is... the search is ignoring the match case even if it finds the match. The object of my code is to take a search string from Names.txt and search the inputFile and if there is a match in .sql file which contains a lot of SQL statements store only the DELETE SQL statement in the outputFile.

Here is my code....

public class NameRemoval
{

public static void main(String[] args) throws Exception
{
File file = new File ("E:\\Workbench\\Name_Removal\\used_input\\Names.txt");
File inputFile = new File ("E:\\Workbench\\Name_Removal\\used_input\\PopulateHighAvailabilityProperties.sql");
File outputFile = new File("E:\\Workbench\\Name_Removal\\output\\PopulateHighAvailabilityProperties_nameWithdrawal.sql");

String name = null;
String newLine = "\n";

Scanner testScanner = new Scanner(file);
testScanner.useDelimiter(newLine);
System.out.println("Name File Pattern" + testScanner.delimiter());

Scanner inputFileScanner = new Scanner(inputFile);
inputFileScanner.useDelimiter(newLine);
System.out.println("Input File Pattern:" + inputFileScanner.delimiter());

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(outputFile));

while (testScanner.hasNext())
{
name = testScanner.nextLine();

System.out.println("Service Name:" + serviceName);
System.out.println(newLine);

while (inputFileScanner.hasNext())
{
String scannedLine = inputFileScanner.next();

System.out.println("Scanned Line:" + scannedLine);
System.out.println(newLine);

if (scannedLine.contains(name) && scannedLine.contains("DELETE"))
{
System.out.println("Scanned Line:" + scannedLine);
System.out.println(newLine);

bufferedWriter.write(scannedLine);
bufferedWriter.write(newLine);
}


}
}
}
}
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i view my output file all i see is an empty file!!!
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Modified the code and it working now. Just wanted to know... Is there any in-built method to bring the scanner object to the beginning of a file? In the previous code i was having this problem. The scanner object inputFileScanner went to the end of the file and then wasn't moving to the beginning of the file. So the consequent searches didn't turn up anything. I modified the code to create a new scanner object every time the the search loop starts!!! Not efficient but working none-the -less!!! Please help me with your suggestions!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!