6

I'm trying to export data into a CSV file through Java and I've got some code to do it but it doesn't seem to be outputting the CSV file. Could someone tell me what's wrong? What I would like to do is rather than saving the file somewhere, I would like it to be directly exported to the user.

EDIT: Just in case it's not clear, I don't want the file to be saved anywhere but would like it to be outputted automatically to the user i.e. they click export and get the "Run/Save results.csv" window and they open the file. Currently the file is getting saved so I know that the method seems to work, just in the opposite way that I want it to.

public static void writeToCSV(List<Map> objectList) {
    String CSV_SEPARATOR = ",";
    try {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("results.csv"), "UTF-8"));
        for (Map objectDetails : objectList) {
            StringBuffer oneLine = new StringBuffer();
            Iterator it = objectDetails.values().iterator();

            while (it.hasNext()) {
                Object value = it.next();

                if(value !=null){
                    oneLine.append(value.toString());
                    }

                if (it.hasNext()) {
                    oneLine.append(CSV_SEPARATOR);
                }
            }
            bw.write(oneLine.toString());
            bw.newLine();
        }
        bw.flush();
        bw.close();
    } catch (UnsupportedEncodingException e) {
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}
3
  • Your code seems all right. Are you sure it is not outputting the file? Try using an absolute file path like "c:\\temp\\results.csv". Commented Aug 24, 2011 at 15:34
  • 3
    Well, you're ignoring all caught exceptions. Is an exception being thrown? Commented Aug 24, 2011 at 15:34
  • Hmm, after further investigation I found that no exceptions are being thrown, but the file is getting saved to the harddrive. I would actually rather like it to open up automatically so that upon hitting the "Export" button, the file opens directly, if you know what I mean? I'm sure this is possible as I've done it before but can't for the life of me remember how I did it. Commented Aug 24, 2011 at 16:00

4 Answers 4

8

I would recommend using a framework like opencsv for that. It also does escaping and quoting for you.

Sign up to request clarification or add additional context in comments.

Comments

1

If you're not getting errors, check the directory where your code is. Without a specific path, your file is being saved there.

EDIT: Since the file is being saved and you want it to open automatically, use

Runtime.getRuntime().exec("results.csv"); 

(For Windows - opens the csv file in the default application for csv files)

Runtime.getRuntime().exec("open results.csv"); 

(For Mac - opens the csv file in the default application for csv files)

5 Comments

Hmm, after further investigation I found that no exceptions are being thrown, but the file is getting saved to the harddrive. I would actually rather like it to open up automatically so that upon hitting the "Export" button, the file opens directly, if you know what I mean? I'm sure this is possible as I've done it before but can't for the life of me remember how I did it.
@WIOijwww Please edit the question to reflect that you need the file to open automatically.
@WIOijwww Your original post made it sound like the file wasn't getting written at all, not that it wasn't opening automatically.
Is this really the best way to do it? I thought Java was platform independent.
The string in the exec() method is the system command to be run, i.e. what you'd run from the command-line of that particular OS to open the file.
0

recommend HSSFWorkbook to easily read and write excel files. http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html

Comments

0

To do that, the CSV reader need to read the memory of your program. This is a little complex thing to do. So, save the file in a temp folder instead. There is no problem to do this sort of thing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.