转换列表< model>列表< string []> Java使用OpenCSV写入CSV

发布于 2025-02-07 03:24:02 字数 534 浏览 3 评论 0原文

我如何将此列表生产

List<GetProductModel> listProduct = new ArrayList<>();

getProductModel

private String ProductName,Description,Price; //getter and setter

放到此列表中

List<String[]> list = new ArrayList<>();

,以便我可以将列表放在

try (CSVWriter writer = new CSVWriter(new FileWriter("c:\\test\\monitor.csv"))) {
        writer.writeAll(list);
    } catch (Exception e) {
        e.printStackTrace();
    }

How do i put this listproduct

List<GetProductModel> listProduct = new ArrayList<>();

GetProductModel

private String ProductName,Description,Price; //getter and setter

to this list

List<String[]> list = new ArrayList<>();

so i can put list to

try (CSVWriter writer = new CSVWriter(new FileWriter("c:\\test\\monitor.csv"))) {
        writer.writeAll(list);
    } catch (Exception e) {
        e.printStackTrace();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风筝有风,海豚有海 2025-02-14 03:24:02

您可以用于循环或流以下循环或流。

List<String[]> list = listProduct.stream()
                .filter(Objects::nonNull)
                .map(getProductModel -> new String[]{getProductModel.getProductName(), getProductModel.getDescription(), getProductModel.getPrice()})
                .collect(Collectors.toList());

you can you use for loop or Streams like:

List<String[]> list = listProduct.stream()
                .filter(Objects::nonNull)
                .map(getProductModel -> new String[]{getProductModel.getProductName(), getProductModel.getDescription(), getProductModel.getPrice()})
                .collect(Collectors.toList());
遗心遗梦遗幸福 2025-02-14 03:24:02
List<GetProductModel> listProduct = new ArrayList<>();

List<String[]> list = new ArrayList<>();

for(GetProductModel x : listProduct) {
    String[] tmpArray = new String[3];
    tmpArray[0] = x.getProductName();
    tmpArray[1] = x.getDescription();
    tmpArray[2] = x.getPrice();
    list.add(tmpArray);
} 
List<GetProductModel> listProduct = new ArrayList<>();

List<String[]> list = new ArrayList<>();

for(GetProductModel x : listProduct) {
    String[] tmpArray = new String[3];
    tmpArray[0] = x.getProductName();
    tmpArray[1] = x.getDescription();
    tmpArray[2] = x.getPrice();
    list.add(tmpArray);
} 
吹梦到西洲 2025-02-14 03:24:02

使用Java Stream API,Lambda映射和PrintWriter写入CSV:

import com.opencsv.CSVWriter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        GetProductModel getProductModel = new GetProductModel();
        getProductModel.setProductName("ProductName");
        getProductModel.setDescription("Description");
        getProductModel.setPrice("Price");

        GetProductModel getProductModel2 = new GetProductModel();
        getProductModel2.setProductName("ProductName2");
        getProductModel2.setDescription("Description2");
        getProductModel2.setPrice("Price2");

        GetProductModel getProductModel3 = new GetProductModel();
        getProductModel3.setProductName("ProductName3");
        getProductModel3.setDescription("Description3");
        getProductModel3.setPrice("Price3");

        List<GetProductModel> getProductModels = new ArrayList<>();
        getProductModels.add(getProductModel);
        getProductModels.add(getProductModel2);
        getProductModels.add(getProductModel3);

        writeToFileUsingPrintWrite(getProductModels);

        writeToFileUsingCSVWriter(getProductModels);
    }

    private static void writeToFileUsingPrintWrite(List<GetProductModel> getProductModels) {
        List<String> productsList = getProductModels
                .stream()
                .map(p -> p.getProductName() + "," + p.getDescription() + "," + p.getPrice())
                .collect(Collectors.toList());

        File csvOutputFile = new File("C:\\products.csv");
        try (
                PrintWriter pw = new PrintWriter(csvOutputFile)) {
            productsList
                    .forEach(pw::println);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void writeToFileUsingCSVWriter(List<GetProductModel> getProductModels) {
        Iterable<String[]> productsList = getProductModels
                .stream()
                .map(p -> new String[]{p.getProductName(), p.getDescription(), p.getPrice()})
                .collect(Collectors.toList());

        CSVWriter writer;
        try {
            writer = new CSVWriter(new FileWriter("C:\\products_2.csv"));
            writer.writeAll(productsList);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Write to csv using Java stream API, Lambda mapping and PrintWriter:

import com.opencsv.CSVWriter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        GetProductModel getProductModel = new GetProductModel();
        getProductModel.setProductName("ProductName");
        getProductModel.setDescription("Description");
        getProductModel.setPrice("Price");

        GetProductModel getProductModel2 = new GetProductModel();
        getProductModel2.setProductName("ProductName2");
        getProductModel2.setDescription("Description2");
        getProductModel2.setPrice("Price2");

        GetProductModel getProductModel3 = new GetProductModel();
        getProductModel3.setProductName("ProductName3");
        getProductModel3.setDescription("Description3");
        getProductModel3.setPrice("Price3");

        List<GetProductModel> getProductModels = new ArrayList<>();
        getProductModels.add(getProductModel);
        getProductModels.add(getProductModel2);
        getProductModels.add(getProductModel3);

        writeToFileUsingPrintWrite(getProductModels);

        writeToFileUsingCSVWriter(getProductModels);
    }

    private static void writeToFileUsingPrintWrite(List<GetProductModel> getProductModels) {
        List<String> productsList = getProductModels
                .stream()
                .map(p -> p.getProductName() + "," + p.getDescription() + "," + p.getPrice())
                .collect(Collectors.toList());

        File csvOutputFile = new File("C:\\products.csv");
        try (
                PrintWriter pw = new PrintWriter(csvOutputFile)) {
            productsList
                    .forEach(pw::println);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void writeToFileUsingCSVWriter(List<GetProductModel> getProductModels) {
        Iterable<String[]> productsList = getProductModels
                .stream()
                .map(p -> new String[]{p.getProductName(), p.getDescription(), p.getPrice()})
                .collect(Collectors.toList());

        CSVWriter writer;
        try {
            writer = new CSVWriter(new FileWriter("C:\\products_2.csv"));
            writer.writeAll(productsList);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文