Writing to a CSV file example with PrintWriter in Java

preview_player
Показать описание
An example of writing to a CSV file in Java with the PrintWriter class.
Рекомендации по теме
Комментарии
Автор

"oh printf... everyones favorite.." that cracked me up man... it's the little things!

yohasakura
Автор

How to avoid converting String value '14E02' to exponential format in CSV

sivaprasath
Автор

does the csv file clears itself after every run?

dhwqbco
Автор

How would you write an entire object without calling each child?

amichaeel
Автор

Guess who got picked up by YouTube algorithms

andywang
Автор

Export:
public <t> void save(String filename) throws IOException {

File file = new File(filename);

try (PrintWriter zapis = new PrintWriter(file)) {
for (Produkt produkt : produktList) {
+ ";"
+ produkt.getWeight() + ";"
+ produkt.getUnitPrice() + ";"
+ produkt.getQuantity());
}
System.out.println("Zapis się powiódł!");
}
catch (IOException ex) {
throw ex;
}
}



System.out.print("Enter file path to save: ");
scanner.nextLine();
String filename = scanner.nextLine();
shoppingList.save(filename);
System.out.println("Saved successfully.");

import:

public List<string> importData(String filename) throws IOException {

List<string> importedList = new ArrayList<>();
File file = new File(filename);

if (file.exists() && file.canRead()) {

try (Scanner scn = new Scanner(file);) {

while (scn.hasNext()) {
String zdanie = scn.nextLine();
String[] parts = zdanie.split(";");
if (parts.length == 4) {
String name = parts[0];
int weight = Integer.parseInt(parts[1]);
double unitPrice = Double.parseDouble(parts[2]);
int quantity = Integer.parseInt(parts[3]);

Produkt produkt = new Produkt(name, weight, unitPrice, quantity);
produktList.add(produkt);
}

}
}
catch (IOException ex) {
throw ex;
}
}
return importedList;
}



System.out.print("Enter file path to import: ");
scanner.nextLine();
String filePath = scanner.nextLine();

System.out.println("Imported successfully.");

milka
join shbcf.ru