gpt4 book ai didi

java - 程序每次运行时都会将 header 添加到 CSV 文件中

转载 作者:行者123 更新时间:2023-12-02 02:35:50 27 4
gpt4 key购买 nike

我一年多前为 Java 编程类(class)编写了这段代码。它是一个基本的食物日志控制台应用程序,可创建 CSV 文件,并可在创建后附加用户的输入。我遇到了一个我永远无法解决的问题。我已经有一段时间没有做过太多编程了,但我对解决方案很好奇。问题是,每次程序运行时,它都会在 CSV 中插入另一个 header 。我只希望它在初始创建时插入,如果文件已经存在,则跳过。

我最初的想法是 else if (file.exists() == true) { 剩余代码块 };在“try” block 中,但它根本不写标题。

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FoodLogWriter {

private static final String COMMA_DELIMITER = ",";
private static final String NEW_LINE_SEPARATOR = "\n";
private static final String FILE_HEADER = "Date,Food Time,Food Item,Calories,Carbohydrates,Sugar,Protein,Fiber,Total Fat";
private static Scanner kb;

public static void writeFoodLog(String fileName, File file) throws IOException {

List<FoodLog> list = new ArrayList<FoodLog>();
FoodLogApp app = new FoodLogApp();

kb = new Scanner(System.in);
String Dinput;
String FTinput;
String FIinput;
double CALinput;
double CARBinput;
double Sinput;
double Pinput;
double TFinput;
double Finput;

int i = 0;

while (i < 1000) {

System.out.print("Enter the date: ");
Dinput = kb.next();

System.out.print("Enter the food time: ");
FTinput = kb.next();

System.out.print("Enter the food item: ");
FIinput = kb.next();

System.out.print("Enter the calories: ");
CALinput = kb.nextDouble();

System.out.print("Enter the carbohydrates: ");
CARBinput = kb.nextDouble();

System.out.print("Enter the sugar: ");
Sinput = kb.nextDouble();

System.out.print("Enter the protein: ");
Pinput = kb.nextDouble();

System.out.print("Enter the fiber: ");
Finput = kb.nextDouble();

System.out.print("Enter the total fat: ");
TFinput = kb.nextDouble();

list.add(app.createFoodLog(Dinput, FTinput, FIinput, CALinput, CARBinput, Sinput, Pinput, Finput, TFinput));

System.out.println("");
System.out.print("Do you have another item to input? ");
String response = kb.next();
System.out.println("");

if (response.equalsIgnoreCase("no")) {

app.printFoodLog(list);
break;

} else {
i++;
}

kb.nextLine();

}

FileWriter fileWriter = null;

try {

fileWriter = new FileWriter(fileName, true);

if (file.exists() == false) {
fileWriter.write(FILE_HEADER.toString());
}
fileWriter.append(NEW_LINE_SEPARATOR);

for (FoodLog FL : list) {
fileWriter.append(String.valueOf(FL.getDate()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(String.valueOf(FL.getFoodTime()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(String.valueOf(FL.getFoodItem()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(String.valueOf(FL.getCalories()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(String.valueOf(FL.getCarbohydrates()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(String.valueOf(FL.getSugar()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(String.valueOf(FL.getProtein()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(String.valueOf(FL.getFiber()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(String.valueOf(FL.getTotalFat()));
fileWriter.append(NEW_LINE_SEPARATOR);
}

System.out.println("CSV file was created successfully.");

} catch (Exception e) {
System.out.println("Error in FoodLogWriter.");
e.printStackTrace();
} finally {

try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
System.out.println("Error while flushing/closing file writer.");
e.printStackTrace();
}
}

}

}

最佳答案

以下是剩余的类(class):

public class FoodLog {

private String Date;
private String FoodTime;
private String FoodItem;
private double Calories;
private double Carbohydrates;
private double Sugar;
private double Fiber;
private double Protein;
private double TotalFat;

public FoodLog(String Date, String FoodTime, String FoodItem, double Calories, double Carbohydrates, double Sugar,
double Fiber, double Protein, double TotalFat) {

// super();
this.Date = Date;
this.FoodTime = FoodTime;
this.FoodItem = FoodItem;
this.Calories = Calories;
this.Carbohydrates = Carbohydrates;
this.Sugar = Sugar;
this.Fiber = Fiber;
this.Protein = Protein;
this.TotalFat = TotalFat;

}

public String getDate() {
return Date;
}

public void setDate(String date) {
Date = date;
}

public String getFoodTime() {
return FoodTime;
}

public void setFoodTime(String foodTime) {
FoodTime = foodTime;
}

public String getFoodItem() {
return FoodItem;
}

public void setFoodItem(String foodItem) {
FoodItem = foodItem;
}

public double getCalories() {
return Calories;
}

public void setCalories(double calories) {
Calories = calories;
}

public double getCarbohydrates() {
return Carbohydrates;
}

public void setCarbohydrates(double carbohydrates) {
Carbohydrates = carbohydrates;
}

public double getSugar() {
return Sugar;
}

public void setSugar(double sugar) {
Sugar = sugar;
}

public double getFiber() {
return Fiber;
}

public void setFiber(double fiber) {
Fiber = fiber;
}

public double getProtein() {
return Protein;
}

public void setProtein(double protein) {
Protein = protein;
}

public double getTotalFat() {
return TotalFat;
}

public void setTotalFat(double totalFat) {
TotalFat = totalFat;
}

public String toString() {
return "FoodLog [Date = " + Date + ", Food Time = " + FoodTime + ", Food Item = " + FoodItem + ", Calories = "
+ Calories + ", Carbohydrates = " + Carbohydrates + ", Sugar = " + Sugar + ", Fiber = " + Fiber
+ ", Protein = " + Protein + ", Total Fat = " + TotalFat + "]";

}

}

import java.io.File;
import java.io.IOException;
import java.util.List;

public class FoodLogApp {

public static void main(String[] args) throws IOException {

File file = new File("FoodLog.csv");
String fileName = System.getProperty("user.home") + "/FoodLog.csv";
FoodLogWriter.writeFoodLog(fileName, file);

}

public FoodLog createFoodLog(String Date, String FoodTime, String FoodItem, double Calories, double Carbohydrates,
double Sugar, double Fiber, double Protein, double TotalFat) {

return new FoodLog(Date, FoodTime, FoodItem, Calories, Carbohydrates, Sugar, Fiber, Protein, TotalFat);
}

public void printFoodLog(List<FoodLog> list) {

for (FoodLog FL : list) {

System.out.println("===========================================");
System.out.println("Food Time : " + FL.getFoodTime());
System.out.println("Food Item : " + FL.getFoodItem());
System.out.println("Calories : " + FL.getCalories());
System.out.println("Carbohydrates : " + FL.getCarbohydrates());
System.out.println("Sugar : " + FL.getSugar());
System.out.println("Protein : " + FL.getProtein());
System.out.println("Fiber : " + FL.getFiber());
System.out.println("Total Fat : " + FL.getTotalFat());
System.out.println("===========================================");
System.out.println("");
}

}

}

关于java - 程序每次运行时都会将 header 添加到 CSV 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46350687/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com