Recently I involved in a project which copies several documentation files in to many subfolder. In order to make the navigation process simple, we thought an index.html file containing the links to those files would be helpful.
A simple java program that read through all directories from the top and created an index for the documents.
Any better solution?
private String getIndexHtml(String folder, boolean dirBrowsingAllowed) {
File root = new File(folder);
System.out.println("Root Folder: "+root.toString());
File file = root;
StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append("<body>");
sb.append("<h1> Document index </h1>");
listFiles(file, dirBrowsingAllowed, sb);
sb.append("</body>");
sb.append("</html>");
return sb.toString();
}
private void listFiles(File file, boolean dirBrowsingAllowed, StringBuffer sb) {
String[] list = file.list();
sb.append("<ul>");
for (String fileOrFolder : list) {
File newFile = new File(file.toString()+File.separator+fileOrFolder);
if(newFile.isHidden()) continue;
if(newFile.isDirectory()){
if(dirBrowsingAllowed){
sb.append("<li>");
sb.append("<a href=\"file:///");
sb.append(newFile.getAbsolutePath());
sb.append("\">");
sb.append(newFile.getPath());
sb.append("</a>");
sb.append("</li>");
}
listFiles(newFile, dirBrowsingAllowed, sb);
} else {
sb.append("<li>");
sb.append("<a href=\"file:///");
sb.append(newFile.getAbsolutePath());
sb.append("\">");
sb.append(newFile.getName());
sb.append("</a>");
sb.append("</li>");
}
}
sb.append("</ul>");
}