wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Display the list of files from a directory and obtain its output in index html format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Display the list of files from a directory and obtain its output in index html format" Watch "Display the list of files from a directory and obtain its output in index html format" New topic
Author

Display the list of files from a directory and obtain its output in index html format

Umme Noor
Greenhorn

Joined: Jul 15, 2013
Posts: 5

I need display a list of files(csv files) from a directory and generate its output in html using PrintWriter of java Io
The index page should be look like 1. Filename( from directory and it should be hyperlink.)
2. 2nd filename
3. 3rd filename etc
Please provide the java code the same.

please help m ASAP.


Thanks in advance

Regards,
Noor
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61450
    
  67

Welcome to the Ranch.

Umme Noor wrote: Please provide the java code the same.


That's not quite how things work here. Give the code a try and post what you come up with. People here will be more than happy to help you with your code, but they're not going to write it for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Umme Noor
Greenhorn

Joined: Jul 15, 2013
Posts: 5
Bear Bibeault wrote:Welcome to the Ranch.

Umme Noor wrote: Please provide the java code the same.


That's not quite how things work here. Give the code a try and post what you come up with. People here will be more than happy to help you with your code, but they're not going to write it for you.

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;


public class NewIndex {

private static ArrayList<File> Process(File file)throws FileNotFoundException, IOException {
// Look up the contents of the directory

ArrayList<File> listAr = new ArrayList<File>();


File[] files = file.listFiles();

// Set up an output stream we can print the table to.
// This is easier than concatenating strings all the time.


StringWriter sout = new StringWriter();

PrintWriter out = new PrintWriter(sout);

// Print the directory name as the page title
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
out.println("<head><meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\"/>");
out.println("<title> Report</title>");
out.println("<style type=\"text/css\">");
out.println("body{background-color:#FFF;color:#000;font-family:OpenSans,sans-serif;font-size:10px;}");
out.println("table{border:0.2em solid #2F6FAB;border-collaoute:collaoute;}");
out.println("th{border:0.15em solid #2F6FAB;padding:0.5em;background-color:#E9E9E9;}");
out.println("td{border:0.1em solid #2F6FAB;padding:0.5em;background-color:#F9F9F9;}</style>");
out.println("</head><body><h1>" + file + ".html" + "</h1>");

// Print out the table
out.println("<table");
out.println("<th>SL-No</th><th>List</th>");


for (int index = 0; index <files.length; index++)
{ listAr.add(files[index]);
//File f = new File(file, files[index]);

out.println("<tr><td>" +
(file.isDirectory() ?
"<a href=\""+file+"\">" + files[index] + "</a>" :
files[index]) +

"</td></tr>");
}
out.println("</table>");

String strOut ="OutputFolder name"
FileWriter fw = new FileWriter(strOut);

fw.close();
return listAr;

}




public static void main(String[] args) {


try {String dirname = "InputDirectory";
File file = new File(dirname);
Process(file);
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}


}
}
Umme Noor
Greenhorn

Joined: Jul 15, 2013
Posts: 5
I was able to rediredt to the output folder however i was not able to print the html content and display the page ,please suggest friends
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61450
    
  67

Where do you want the page to display?
Umme Noor
Greenhorn

Joined: Jul 15, 2013
Posts: 5
Bear Bibeault wrote:Where do you want the page to display?

Need to display the output in browser and whatever generated html should be stored in a specific folder
Umme Noor
Greenhorn

Joined: Jul 15, 2013
Posts: 5
Umme Noor wrote:
Bear Bibeault wrote:Where do you want the page to display?

Need to display the output in browser and whatever generated html should be stored in a specific folder

After running the program it default opens in an browser however nthng is been displayed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display the list of files from a directory and obtain its output in index html format