[英]Using Netbeans writing to a csv file not working
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.我目前正在开发一个车载云程序,该程序获取客户信息和车主信息,并将它们存储在单独的 .csv 文件中。 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.
我使用一个数组来分隔每个对象,以便更容易地放入 csv 文件,但问题是,当我运行程序时,它执行但不写入文件。 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")
我也尝试不使用文件的直接路径(“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 - 写入 .csv 文件代码
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();
}
You are neither using flush() which would write the data to your file nor close() which flushes first and then closes the writer.您既没有使用将数据写入文件的 flush() 也没有使用先刷新然后关闭写入器的 close() 。 In general you should always close any writer/stream/etc.
一般来说,您应该始终关闭任何编写器/流/等。 that you opened before.
你之前打开的。
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.
如果文件位于名为资源的文件夹中,则示例可能是“resources/data.csv”。 In your case it may be "src/data.csv".
在您的情况下,它可能是“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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.