Writing to a CSV file example with PrintWriter in Java

Поділитися
Вставка
  • Опубліковано 27 січ 2025

КОМЕНТАРІ • 13

  • @yohasakura7419
    @yohasakura7419 3 роки тому +1

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

  • @christinedai9239
    @christinedai9239 4 роки тому +4

    Thanks this is a huge help.

  • @sketraj8635
    @sketraj8635 Рік тому

    ice! its actually worked!!!!! 😘

  • @אושרעובדיה
    @אושרעובדיה 11 місяців тому

    does the csv file clears itself after every run?

  • @sivaprasath3401
    @sivaprasath3401 4 роки тому +1

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

  • @amichaeel
    @amichaeel 2 роки тому

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

  • @dev_troy8903
    @dev_troy8903 Рік тому

    nice, what keyboard are you using ?

  • @andywang2483
    @andywang2483 3 роки тому +1

    Guess who got picked up by UA-cam algorithms

  • @einhengst2525
    @einhengst2525 3 роки тому

    You sound like dvr =:)

  • @milka7095
    @milka7095 Рік тому

    Export:
    public void save(String filename) throws IOException {
    File file = new File(filename);
    try (PrintWriter zapis = new PrintWriter(file)) {
    for (Produkt produkt : produktList) {
    zapis.println(produkt.getName() + ";"
    + 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 importData(String filename) throws IOException {
    List 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();
    shoppingList.importData(filePath);
    System.out.println("Imported successfully.");