deadhand
Messages postés152Date d'inscriptiondimanche 15 octobre 2006StatutMembreDernière intervention27 août 20103 20 janv. 2010 à 11:01
Thanks ! I completly forget this !
cs_simonreno
Messages postés3Date d'inscriptionvendredi 8 décembre 2000StatutMembreDernière intervention20 janvier 2010 20 janv. 2010 à 09:31
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class FindStringInFiles
{
public static boolean validFileExtension(final String[] fileExt, final String fileName)
{
for (final String ent : fileExt)
{
if (fileName.lastIndexOf('.') == -1)
{
return false;
}
final String fileNameTmp = fileName.substring(fileName.lastIndexOf('.'));
if (fileNameTmp.compareTo(ent) == 0)
{
return true;
}
}
return false;
}
public static Map<File, Integer> getOccurrences(final String path, final String[] fileExtensions, final String targetString)
{
final Map<File, Integer> resultFileNbOccurence = new HashMap<File, Integer>();
int nbOccurence = 0;
File dir = new File(path);
for (File file : dir.listFiles())
{
if (!file.isDirectory())
{
if (validFileExtension(fileExtensions, file.getName()))
{
System.out.println("Look into file : " + file.getName());
InputStream ips = null;
InputStreamReader ipsr = null;
BufferedReader br = null;
try
{
ips = new FileInputStream(file);
ipsr = new InputStreamReader(ips);
br = new BufferedReader(ipsr);
String ligne;
while ((ligne = br.readLine()) != null)
{
if (ligne.contains(targetString))
{
nbOccurence++;
}
}
if (nbOccurence > 0)
{
resultFileNbOccurence.put(file, new Integer(nbOccurence));
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error while processing file: "+file.getName()+". Exception msg: "+e.getMessage());
}
finally
{
try
{
if (br != null)
{
br.close();
}
if (ipsr != null)
{
ipsr.close();
}
if (ips != null)
{
ips.close();
}
}
catch (Exception e1)
{
// ignore
}
}
}
}
else
{
resultFileNbOccurence.putAll(getOccurrences(file.getAbsolutePath(), fileExtensions, targetString));
}
}
return resultFileNbOccurence;
}
public static void main(String[] args)
{
final String path = "./src";
final String[] fileExtensions = { ".java", ".properties", ".xml" };
final String searchString = "stz";
System.out.println("Start looking for ["+searchString+"] in files with extension: ["+fileExtensions+"] - from path: "+path);
final Map<File, Integer> result = getOccurrences(path, fileExtensions, searchString);
int totalNbOccurrences = 0;
for (Integer nbOccurencePerFile : result.values())
{
totalNbOccurrences += nbOccurencePerFile.intValue();
}
System.out.println(totalNbOccurrences + " occurence(s) of "+searchString+" found in files with extension: ["+fileExtensions+"] - from path: "+path);
System.out.println(result.size()+" files found: "+result.keySet());
}
}
cs_simonreno
Messages postés3Date d'inscriptionvendredi 8 décembre 2000StatutMembreDernière intervention20 janvier 2010 20 janv. 2010 à 09:09
great, just a few changes are required to output list of files, instead of a number, where string occurences have been found.
Else, think about closing the input stream and buffer reader to prevent from files lock.
Greatings
20 janv. 2010 à 11:01
20 janv. 2010 à 09:31
20 janv. 2010 à 09:09
Else, think about closing the input stream and buffer reader to prevent from files lock.
Greatings