private String pattern; // Defines pattern for file name ending
/*
Author: Sandeep
Description: Method walk throughout directory recursively
*/
public void fileWalkThrough ( String dirPath ) throws IOException {
File dir = new File(dirPath);
File[] paths = dir.listFiles();
List<File> files = Arrays.asList(paths);
Stream<File> stream = files.stream(); // Java 8 Feature
stream.filter(file -> file.isDirectory()).forEach(dirs -> {
try {
findReplaceEnvFiles( dirs.getAbsolutePath() );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
findReplaceEnvFiles( dirPath ); // Calls the root Directory
}
/*
Author: Sandeep
Description: identifies the filenames which is ending with pattern
*/
private void findReplaceEnvFiles ( String dirPath ) throws IOException{
File dir = new File(dirPath);
File[] paths = dir.listFiles();
List<File> files = Arrays.asList(paths);
System.out.println(“\n\nFiles with patterns ” + pattern + ” in directory will be processed ” + dirPath + “\n”);
Stream<File> stream = files.stream(); // Java 8 feature
stream.filter(file -> file.getAbsolutePath().matches(“.*” + pattern + “([.]).*$”)).forEach(
value -> {
try {
copyFileUsingJava(value, pattern );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}
/*
Author: Sandeep
Description: Replaces the content of pattern files from the content of patterned file
*/
private void copyFileUsingJava(File source, String pattern) throws IOException {
String dest = source.getAbsolutePath().replaceAll(pattern, “”);
File destFile = new File(dest);
if ( !destFile.exists() ) {
if (destFile.createNewFile()) {
System.out.println(dest + ” created succesfully. “);
} else {
System.out.println(dest + ” didn’t creation failed. “);
}
}
System.out.println(dest +” content will be replaced with ” + source.getAbsolutePath());
Files.copy(source.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING); // Java 7 Feature
}
Happy Java 8 Programming !!

Leave a comment