使用Spring Boot应用程序读取csv文件

时间:2017-05-24 09:15:51

标签: java spring csv

我正在尝试将我编写的Java程序转换为将csv文件读入Spring引导应用程序,但不断获得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();
    }
}

2 个答案:

答案 0 :(得分:0)

代码存在很多问题。我猜第一个是

电话

(start_date..end_date).map { |date| [date.to_s, ''] }.to_h
#=> {"2017-05-24"=>"", "2017-05-25"=>"", "2017-05-26"=>"", "2017-05-27"=>"", "2017-05-28"=>"", "2017-05-29"=>""}

调用obj.display(); ,但永远不会分配listp.output();。既不是配置也不是代码。

答案 1 :(得分:0)

问题已经解决了。 classes和beans.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

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");

        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);
        }
    }

}