Scenerio: Suppose you have to move millions of files from archive drive to some other drive after doing some manipulation like zipping some files together and all (this is different utility) with files in this case you want all the file list
Answer: We are going to use Files.walk() cool feature came in Java 8 . No much loops and all just already available methods you have to use thats all .
package com.aem.community.core.servlets;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.json.JSONArray;
import org.json.JSONException;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
@Component(service=Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths="+ "/bin/filenames"
})
public class FilelistAll extends SlingSafeMethodsServlet {
private static final long serialVersionUid = 1L;
@Override
protected void doGet(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws ServletException, IOException {
final Resource resource = req.getResource();
String path = "E:\\Proxy_Project\\Morning";
// File folder = new File(path);
Stream<Path> files = Files.walk(Paths.get(path));
Stream<Path> pathStream = files.filter(pathchecking -> pathchecking.toString().endsWith(".pdf") || pathchecking.toString().endsWith(".json"));
JSONArray pdflist = new JSONArray();
JSONArray jsonlist = new JSONArray();
//pathStream.forEach(System.out::println);
int rowcount = 1;
pathStream.forEach(
(i) -> {
String unixpath = String.valueOf(i).replace("\\", "/");
if (unixpath.endsWith(".pdf")) {
// JSONArray pdflist=new JSONArray();
//pdflist.put("//"+unixpath);
pdflist.put(unixpath);
} else if (unixpath.endsWith(".json")) {
jsonlist.put(unixpath);
}
// System.out.println(i);
// try {
// resp.getWriter().write(String.valueOf(i).replace("\\", "/"));
// } catch (IOException e) {
// e.printStackTrace();
// }
}
);
//now we got the list of pdfs and json now put in workbook
try {
XSSFWorkbook fileworkbook = new XSSFWorkbook();
XSSFSheet fileSheet = fileworkbook.createSheet("filelist");
XSSFRow headingRow = fileSheet.createRow(0);
headingRow.createCell(0).setCellValue("mainfile");
headingRow.createCell(1).setCellValue("jsondocument");
headingRow.createCell(2).setCellValue("htmldocument");
for (int i = 1; i < pdflist.length(); i++) {
XSSFRow otherRow = fileSheet.createRow(i);
otherRow.createCell(0).setCellValue(pdflist.getString(i - 1));
//just to check if corresponding metadata file(json exist or not)
String jsonpathmaking = pdflist.getString(i - 1).substring(0, pdflist.getString(i - 1).length() - 3);
String jsonfilepath=jsonpathmaking.concat("json");
File jsonfile=new File(jsonfilepath);
if(jsonfile.exists()){
otherRow.createCell(1).setCellValue(pdflist.getString(i - 1).substring(0,pdflist.getString(i - 1).length()-3)+"json");
otherRow.createCell(2).setCellValue(pdflist.getString(i - 1).substring(0,pdflist.getString(i - 1).length()-3)+"html");
}
}
//putting code to write the workbook in stream
String reportName = "allfiles.xlsx";
resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
resp.setHeader("Content-Disposition", "attachment; filename=" + reportName);
ServletOutputStream outputStream = null;
try {
outputStream = resp.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
fileworkbook.write(outputStream);
fileworkbook.close();
// resp.setContentType("text/plain");
// resp.getWriter().write("Title = " + resource.adaptTo(ValueMap.class).get("jcr:title"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
if you want to exclude any particular level directories files ?
It can be in any case suppose that folder acces you dont have or sometines you need that whatever root path you give inside that all subdirectory files only you want to make list not the root level files …supooose myroot(folder name)..in this level two pdf and then inside myroot folder three to 4 directories /subfolder (having files .pdf,.json) so you dont need to consider the files which are on same level of myroot folder ?
How ypu will do that ? Skip you can use
/*
* Copyright 2015 Adobe Systems Incorporated
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.aem.community.core.servlets;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.json.JSONArray;
import org.json.JSONException;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
@Component(service=Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths="+ "/bin/filenames"
})
public class FilelistAll extends SlingSafeMethodsServlet {
private static final long serialVersionUid = 1L;
@Override
protected void doGet(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws ServletException, IOException {
final Resource resource = req.getResource();
//String path = "E:\\any_Project\\Morning";
String path=req.getParameter("pathName");
// File folder = new File(path);
Stream<Path> files = Files.walk(Paths.get(path));
// Predicate <String> predicate = element -> element.startsWith("P");
//long count = colours.stream().filter(predicate).count();
Stream<Path> pathStream = files.filter(pathchecking -> pathchecking.toString().endsWith(".pdf") );
//Stream<Path> filesSecond = Files.walk(Paths.get(path));
Stream<Path> skip = pathStream.skip(Files.walk(Paths.get(path), 1).filter(pathchekingagain -> pathchekingagain.toString().endsWith(".pdf")).count());
JSONArray pdflist = new JSONArray();
JSONArray jsonlist = new JSONArray();
//pathStream.forEach(System.out::println);
int rowcount = 1;
//pathStream.forEach(
skip.forEach(
(i) -> {
String unixpath = String.valueOf(i).replace("\\", "/");
if (unixpath.endsWith(".pdf")) {
// JSONArray pdflist=new JSONArray();
//pdflist.put("//"+unixpath);
pdflist.put(unixpath);
}
// else if (unixpath.endsWith(".json")) {
//
//
// jsonlist.put(unixpath);
// }
// System.out.println(i);
// try {
// resp.getWriter().write(String.valueOf(i).replace("\\", "/"));
// } catch (IOException e) {
// e.printStackTrace();
// }
}
);
//now we got the list of pdfs and json now put in workbook
try {
XSSFWorkbook fileworkbook = new XSSFWorkbook();
XSSFSheet fileSheet = fileworkbook.createSheet("filelist");
XSSFRow headingRow = fileSheet.createRow(0);
headingRow.createCell(0).setCellValue("mainfile");
headingRow.createCell(1).setCellValue("jsondocument");
headingRow.createCell(2).setCellValue("htmldocument");
for (int i = 1; i < pdflist.length(); i++) {
XSSFRow otherRow = fileSheet.createRow(i);
otherRow.createCell(0).setCellValue(pdflist.getString(i - 1));
//just to check if corresponding metadata file(json exist or not)
String jsonpathmaking = pdflist.getString(i - 1).substring(0, pdflist.getString(i - 1).length() - 3);
String jsonfilepath=jsonpathmaking.concat("json");
File jsonfile=new File(jsonfilepath);
if(jsonfile.exists()){
otherRow.createCell(1).setCellValue(pdflist.getString(i - 1).substring(0,pdflist.getString(i - 1).length()-3)+"json");
otherRow.createCell(2).setCellValue(pdflist.getString(i - 1).substring(0,pdflist.getString(i - 1).length()-3)+"html");
}
}
//putting code to write the workbook in stream
String reportName = "allfiles.xlsx";
resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
resp.setHeader("Content-Disposition", "attachment; filename=" + reportName);
ServletOutputStream outputStream = null;
try {
outputStream = resp.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
fileworkbook.write(outputStream);
fileworkbook.close();
// resp.setContentType("text/plain");
// resp.getWriter().write("Title = " + resource.adaptTo(ValueMap.class).get("jcr:title"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}