i need to find all the files in the directory and sub directory that have a text file..within this text file there is a string called average with some value.. the code below works fine for a single directory...but i need to find the same text files in the sub directory .....can someone please help in how to traverse within the sub directories..
import java.io.*;
import java.util.*;
import java.io.FileWriter;
import java.io.IOException;
//chets pgm
public class GenerateCsv
{
public static void main(String [] args)
{
generateCsvFile("C:\\DiffxNs.csv"); //creation of csv file
}
private static void generateCsvFile(String sFileName)
{
try
{
FileWriter writer = new FileWriter(sFileName);
//FileInputStream fstream = new FileInputStream("C:\\Projects\\Broker\\result-parse\\results\\1kb\\Sub.txt");//extraction of txt file
String dirLocation = "C:\\Projects\\Broker\\AIX\\IBMLabTests_Mar12\\Non-Pers_Pers_Result_AIX_Set2\\Non-Pers_Pers_Result_AIX_Set2\\DurableVsNonDurable\\Plain\\Durable\\Persistent\\Sub";//change path here
File dir = new File("C:\\Projects\\Broker\\AIX\\IBMLabTests_Mar12\\Non-Pers_Pers_Result_AIX_Set2\\Non-Pers_Pers_Result_AIX_Set2\\DurableVsNonDurable\\Plain\\Durable\\Persistent\\Sub"); //COUNT of number of files
String[] children = dir.list();
int count=0;
for (int i=0; i<children.length; i++)
{ // Get filename of file or directory
String filename = children[i];
String NewDir = dirLocation +"\\"+filename;
FileInputStream fstream = new FileInputStream(NewDir);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
{
// Print the content on the console
//System.out.println (strLine);
String start = "average"; //string to be searched
if(i==0)
{
if (strLine.startsWith(start))
{
writer.append("SI.NO");
writer.append(',');
writer.append("AVERAGE");
writer.append(',');
writer.append("FILENAME");
writer.append('\n');
count = count+1;
String aString = Integer.toString(count);
writer.append(aString);
writer.append(',');
String value = strLine;
value=strLine.substring(14);
writer.append(value);
writer.append(',');
writer.append(children[i]);
}
else
{
System.out.println("nope");
}
}
else
{
if (strLine.startsWith(start))
{
writer.append('\n');
//writer.append(strLine);
count = count+1;
String aString = Integer.toString(count);
writer.append(aString);
writer.append(',');
String value = strLine;
value=strLine.substring(14);
writer.append(value);
writer.append(',');
writer.append(children[i]);
}
else
{
System.out.println("nope");
}
//writer.append(',');
//writer.append(strLine);
}
}//Close the input stream
in.close();
}
System.out.println("Your file has been written");
writer.flush();
writer.close();
}
//System.out.println(NewDir);
//System.out.println(count);
catch(Exception e)
{
e.printStackTrace();
}
}
// count=count+1;
// Get the object of DataInputStream
}