Question

I have a file that is stored in my netbeans project in the following path:

ReportCSV/ReportDownload.csv

And I have the following code in my struts action class to write to this file:

CSVWriter writer = new CSVWriter(new FileWriter("C:\\isis\\src\\main\\webapp\\ReportCSV\\ReportDownload.csv"), '\t');

//feed in your array (or convert your data to an array)
String[] entries = "first#second#third".split("#");
writer.writeNext(entries);
writer.close();

Over here, specifying the exact file path works but I would like to make this path relative. I've tried "\\ReportCSV\\ReportDownload.csv" but it doesn't work. Any idea what I can do? Any help is appreciated! Thank you :)

Was it helpful?

Solution

If you want a relative path to your application web, you need javax.servlet.ServletContext.

With the class org.apache.struts2.ServletActionContext, you can try:

ServletContext context = ServletActionContext.getServletContext();
String path = context.getRealPath("/ReportCSV/ReportDownload.csv");
CSVWriter writer = new CSVWriter(new FileWriter(path), '\t');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top