0

I'm trying to generate and download csv file in a Java project that use an old framework called Echo Studio 3, with Tomcat.

here is my code:

List<Object> reportCount = this.getReport(accountId);
ArrayList<String[]> result = new ArrayList<String[]>();
for (Object list: reportCount) {
    String[] rowReport = (String[]) list;
    result.add(new String[]{String.valueOf(rowReport[0]),String.valueOf(rowReport[1])});
}

File filename = new File("report.csv");
FileWriter fw = new FileWriter(filename);
fw.append("Name");
fw.append(',');
fw.append("Count");
fw.append('\n');
for (String[] ls: result){
    fw.append(ls[0].toString());
    fw.append(',');
    fw.append(ls[1].toString());
    fw.append('\n');
}
try {
    fw.flush();
    fw.close();
} catch (Exception e) {
    System.out.println("Error while flushing/closing fileWriter !!!");
    e.printStackTrace();
}

When the code is triggered, nothing happens, and i have no error message. when i run the debug mode, the result is filled with data, and the breakpoint pass throw the flush(), and no error, and the client is not getting the generated file, did I miss something?

Marc El Bichon
  • 397
  • 1
  • 7
  • 24

1 Answers1

0

You're writing the file to the current working directory, which may be something you don't expect if you're running the program in a container like Tomcat.

Given you've seen the program execute the calls, i'd test that hypothesis by changing the file path to an absolute path, to make sure that the code is actually writing correctly. If it does, then it's just a path issue.

You could also add a call to find the current working directory 1.

Community
  • 1
  • 1
gus
  • 961
  • 6
  • 4