Search code examples
javafilefilewriter

Why is FileWriter not writing to "output.csv" but does write to the exact path "C:\\path\\output.csv"?


There are no errors thrown. It will work if I specify the exact path but not if I just say "output.csv"

ResultSet rs = statement.executeQuery("select * from geninfo");
                try {
                    CSVWriter writer = new CSVWriter(new FileWriter(new File("output.csv")));
                    writer.writeAll(rs, true);
                    writer.flush();
                    writer.close();
                } catch (IOException ex) {
                    Logger.getLogger(SQLite.class.getName()).log(Level.SEVERE, null, ex);
                }

Solution

  • If you just say output.csv it should write to the working directory, which is usually the directory you started Java from, but that can vary depending on if you're starting from an IDE.

    You can check the actual file it's using though with:

    new File("output.csv").getAbsolutePath()