0

I am currently working on a vehicular cloud program that takes client information and takes vehicle owner information and stores them in separate .csv files. I am using an array to separate each object making it easier to put in the csv file but the problem is, when ever i run through the program it executes but does not write to file. I use a submit button to store the info in an array that allows for later use when writing to the file. I tried not using the direct path to the file as well ex.("ownerLog.csv")

-SubmitButton code

 private void submit2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     
 

   String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
            String[] tempArr = {OID.getText(), VModel.getText(), (String)Vehcolors.getSelectedItem(),
                                vplate.getText(), (String)Rday.getSelectedItem(), (String)RMonths.getSelectedItem(), timeStamp};
            //adding the temporary data array to the total client entries for file writing at later point 
            ownerEntries.add(tempArr);

-Write to .csv file code

try {
        
                try (FileWriter csvWriter = new FileWriter("C:\\Users\\juals\\Documents\\NetBeansProjects\\GUIFormExamples\\src\\ownerLog.csv")) {
                    csvWriter.append("Owner id");
                    csvWriter.append(",");
                    csvWriter.append("Vehicle Model");
                    csvWriter.append(",");
                    csvWriter.append("Vehicle Color");
                    csvWriter.append(",");
                    csvWriter.append("Vehicle Plate Number");
                    csvWriter.append(",");
                    csvWriter.append("Approx. Residency Days");
                    csvWriter.append(",");
                    csvWriter.append("Approx Residency Months");
                    csvWriter.append(",");
                    csvWriter.append("Timestamp");
                    csvWriter.append("\n");
                    
                    for(String[] entry: ownerEntries) {
                        System.out.println(String.join(",", entry));
                        csvWriter.append(String.join(",", entry));
                        csvWriter.append("\n");
                    }
                }
        
    } catch (IOException e) {
        e.printStackTrace();
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jualston
  • 37
  • 6

2 Answers2

0

You are neither using flush() which would write the data to your file nor close() which flushes first and then closes the writer. In general you should always close any writer/stream/etc. that you opened before.

Joachim Rohde
  • 5,915
  • 2
  • 29
  • 46
  • Hi i tried doing csvWriter.flush(); after i append and even close the file but it still doesnt write. Does the flush function go after every string i append? and im assuming close() comes at the end once everything is done. – Jualston Oct 21 '20 at 08:38
0

The string filename is the path to your file. An example could be "resources/data.csv" if the file is in a folder called resources. In your case it may be "src/data.csv". Here is some example code:

PrintWriter out; // a field

public void writeCsvFile(String filename) {
    out = null;
        
    try {
        File file = new File(filename);
        //System.out.println(file.getAbsolutePath());
        FileWriter csvWriter = new FileWriter(file, true);
        out = new PrintWriter(csvWriter);
        out.append("\n");

        // Appending to CSV file
        out.append("Owner id");
        out.append(",");
        out.append("Vehicle Model");
        out.append(",");
        out.append("Vehicle Color");
        out.append(",");
        out.append("Vehicle Plate Number");
        out.append(",");
        out.append("Approx. Residency Days");
        out.append(",");
        out.append("Approx Residency Months");
        out.append(",");
        out.append("Timestamp");
        out.append("\n");

        for(String[] entry: ownerEntries) {
            System.out.println(String.join(",", entry));
            out.append(String.join(",", entry));
        }
            
        out.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if(out != null) {
            out.close();
        }
    }
}

Here is a minimal example:

PrintWriter out; // a field

public void writeCsvFile(String filename) {
    out = null;
    
    try {
        File file = new File(filename);
        //System.out.println(file.getAbsolutePath());
        FileWriter csvWriter = new FileWriter(file, true);
        out = new PrintWriter(csvWriter);

        // Appending to CSV file
        out.append("First value");
        out.append(",");
        out.append("Second value");
        out.append(",");
        out.append("Third value");
        
        out.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if(out != null) {
            out.close();
        }
    }
}
Peter Alsen
  • 320
  • 2
  • 5
  • 15