簡體   English   中英

使用Spring Boot應用程序讀取CSV文件

[英]Reading a csv file with Spring Boot application

我正在嘗試將為讀取csv文件而編寫的Java程序轉換為Spring Boot Application,但始終收到NullPointerException。 我只想打印出csv文件的內容。 這是代碼:

beans.xml中

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
   <bean id="Assembler" class="sample.spring.chapter01.Assembler">          
        <property name="infopieces" value="classpath:1erelistearticle2.csv"></property>         
   </bean>
   <bean id="PieceDAO" class="sample.spring.chapter01.PieceDAO">
        <property name="listp" ref="Piece"></property>
    </bean>
   <bean id = "Piece" class = "sample.spring.chapter01.Piece"/>   
</beans>

Minapp.java

package sample.spring.chapter01;

import java.io.FileNotFoundException;
    import java.io.IOException;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;

public class MinApp {
    private static ApplicationContext context;
    private static Logger logger = Logger.getLogger(MinApp.class);

    public static void main(String[] args)  throws FileNotFoundException, IOException{
        context=new ClassPathXmlApplicationContext("Beans.xml");
        Resource resource =context.getResource("classpath:1erelistearticle2.csv");

        Assembler obj=(Assembler) context.getBean("Assembler");


        obj.display();

        try {
            ArrayList<Piece> listp=obj2.getList();
            for (Piece p:listp) {
                System.out.println(p);
            }
        } catch (IOException ef) {



    }
}

Assembler.java

package sample.spring.chapter01;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.opencsv.CSVReader;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.core.io.Resource;

public class Assembler {
    private PieceDAO listp;
    //private CSVParser parser;
    private Resource infopieces;

    public void setlistp(PieceDAO lp) throws FileNotFoundException, IOException {
        fill();     
    }

    public void setinfopieces(Resource csvFile){
        this.infopieces = csvFile;
    }

    public PieceDAO getlistp() {
        return listp;
    }

    public CSVParser getinfocsv() throws FileNotFoundException, IOException{
         //   BufferedReader br = new BufferedReader(new FileReader("classpath:1erelistearticle.csv"));

            //BufferedReader br = new BufferedReader(new FileReader("classpath:1erelistearticle.csv"));

            InputStream is=infopieces.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));              

            //ClassLoader classloader=Thread.currentThread().getContextClassLoader();
            //InputStream is=classloader.getResourceAsStream("classpath:1erelistearticle.csv");
            //CSVReader cr=new CSVReader(is);
            CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(br);
            //CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(is);
            //for (CSVRecord record: parser) {
            //    System.out.println(record.get("référence ascensoriste"));
            //}
            return parser;
    }

    public void fill() throws FileNotFoundException, IOException{

        CSVParser parser=getinfocsv();
        for (CSVRecord record:parser) {
            String ref=record.get("référence ascensoriste").trim();
            String asc=record.get("ascensoriste").trim();
            String desc=record.get("Description article").trim();                
            String prix=record.get("Pv");
            String category=record.get("Familie").trim();
            //System.out.println(category);
            Piece lift_comp=new Piece();
            lift_comp.setasc(asc);
            lift_comp.setdesc(desc);
            lift_comp.setref(ref);
            lift_comp.setprice(prix);
            lift_comp.settype(category);
            lift_comp.setinfo();
            listp.addPiece(lift_comp);
        }
    }

    public void display() {
        listp.output();
    }
}

代碼有很多問題。 我想第一個是

通話

obj.display();

哪個調用listp.output(); listp從未分配。 無論是從配置還是從代碼。

問題已經解決。 這些類和bean.xml如下,並使用Spring框架讀取csv文件。

beans.xml中

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="PieceDAO" class="sample.spring.chapter01.PieceDAO">
        <property name="listp" ref="Piece"></property>              
   </bean>
   <bean id = "Piece" class = "sample.spring.chapter01.Piece"/>   
</beans>

Minapp.java

包sample.spring.chapter01;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;

public class MinApp {
    private static ApplicationContext context;
    private static Logger logger = Logger.getLogger(MinApp.class);

    public static void main(String[] args)  throws FileNotFoundException, IOException{
        context=new ClassPathXmlApplicationContext("Beans.xml");
        Resource resource =context.getResource("classpath:1erelistearticle2.csv");

        PieceDAO obj=(PieceDAO) context.getBean("PieceDAO");
        obj.fill(resource);
        obj.display();

    }
}

PieceDAO.java

package sample.spring.chapter01;

import java.util.List;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.core.io.Resource;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class PieceDAO {
    private List<Piece> listp;
    //private Resource resource;

    public PieceDAO() {


    }

    public void setlistp(Piece p) throws FileNotFoundException, IOException{
        listp=new ArrayList<Piece>();
        //this.fill(resource);
    }

    public Piece getlistp(Piece p) {
        for (Piece currp:listp) {
            if (currp.equals(p)) {
                return currp;
            }
        }
        return null;

    }


    public void fill(Resource resource) throws FileNotFoundException, IOException{
        InputStream is=resource.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));              


        CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(br);

        for (CSVRecord record:parser) {
            String ref=record.get("référence ascensoriste").trim();
            String asc=record.get("ascensoriste").trim();
            String desc=record.get("Description article").trim();                
            String prix=record.get("Pv");
            String category=record.get("Familie").trim();
            //System.out.println(category);
            Piece lift_comp=new Piece();
            lift_comp.setasc(asc);
            lift_comp.setdesc(desc);
            lift_comp.setref(ref);
            lift_comp.setprice(prix);
            lift_comp.settype(category);
            lift_comp.setinfo();
            listp.add(lift_comp);
        }


    }

    public void display() {
        for (Piece p:listp) {
            System.out.println(p);
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM