【问题标题】:Spliting each 1000 rows of my CSV file to a XML File in JAVA将我的 CSV 文件的每 1000 行拆分为 JAVA 中的 XML 文件
【发布时间】:2020-08-20 02:25:15
【问题描述】:

我有一个 java 程序,它会读取我的 CSV 文件并将其转换为 XML 文件,但问题是我的 CSV 文件有很多行并且程序无法将所有行都转换为 XML ,所以我需要阅读我的每 1000 行 csv 并为每个行创建一个 XMl

以下是我的代码,但我无法读取每 1000 行...


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

public class XMLMainApp {

    public static void main(String[] args) throws FileNotFoundException {

        UteXmlComunicazione UteXmlComunicazione = new UteXmlComunicazione();        

        //read the csv file and collect all objects
        try {

            String inputfile = "sample.csv"; //  Source File Name.  
              double nol = 1000.0; //  No. of lines to be split and saved in each output file.  
              File file = new File(inputfile);  
              Scanner scanner = new Scanner(file);  
              int count = 0;  
              while (scanner.hasNextLine())   
              {  
               scanner.nextLine();  
               count++;  
              }  
              System.out.println("Lines in the file: " + count);     // Displays no. of lines in the input file.  

              double temp = (count/nol);  
              int temp1=(int)temp;  
              int nof=0;  
              if(temp1==temp)  
              {  
               nof=temp1;  
              }  
              else  
              {  
               nof=temp1+1;  
              }  
              System.out.println("No. of files to be generated :"+nof); // Displays no. of files to be generated.  

              FileInputStream fstream = new FileInputStream(inputfile);
              DataInputStream in = new DataInputStream(fstream);  

                BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
                 String strLine;  

             for (int j=1;j<=nof;j++)  
              {  

            while ((line = br.readLine()) != null) {    //get every single line individually in csv file
                String[] value = line.split(";");   //collect the comma separated values into array
                datiCliente datiCliente = new datiCliente();            
                datiCliente.setcfPiva(value[0]);
               ... //  giving the related Node to each of my fields.

            }

        UteXmlComunicazione.setdatiFornitoraClienteList(listForCliente);

        for (int i=1;i<=nol;i++)  
        {  
        //marshaling with java 
        try {

            JAXBContext jaxbContext = JAXBContext.newInstance(UteXmlComunicazione.class);
            javax.xml.bind.Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true)
            jaxbMarshaller.marshal(UteXmlComunicazione, new File("C:/NewFolder/output"+j+".xml"));
            jaxbMarshaller.marshal(UteXmlComunicazione, System.out);

        } 
        catch (JAXBException e) {
            e.printStackTrace();
            System.out.println("File is not found");
        }
        }
        }


        }
        catch (IOException e) {
            e.printStackTrace();
    }
    }    
}


当我运行此代码时,它会创建 180 个文件(这是正确的),但在每个文件中我都有我的所有行,而不仅仅是 1000 行......而且我无法打开它。 谁能给我一个解决方案?

【问题讨论】:

    标签: java xml csv split marshalling


    【解决方案1】:

    尝试以下解决方案,此示例与您之前的问题有关。首先,您应该将 csv 文件拆分为多个 csv 文件,包括预期的行数。然后,分别获取准备好的 scv 文件并创建 xml 文件

    People.java

    @XmlRootElement(name="people")
    @XmlAccessorType (XmlAccessType.FIELD)
    public class People {
    
        @XmlElement(name="person")
        private List<Person> listPerson;
    
        public List<Person> getListPerson() {
            return listPerson;
        }
        public void setListPerson(List<Person> listPerson) {
            this.listPerson = listPerson;
        }
    }
    

    Person.java

    @XmlRootElement(name="person")
    @XmlAccessorType (XmlAccessType.FIELD)
    public class Person {
    
        private String id;
        private String firstName;
        private String lastName;
    
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getFirstName() {
            return firstName;
        }
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }
    

    将csv文件拆分成多个csv文件,分别创建xml文件,

    public class Marshaller {
    
        int rowCount;
        int fileCount = 1;
        PrintWriter writer;
        int lineCount;
        int index;
        String line;
        ArrayList<Person> list;
    
        public static void main(String[] args) {
            Marshaller marshaller = new Marshaller();
            marshaller.readCSV();   //split csv file into multiple csv files
            marshaller.readMultipleCSV(); //create XML files using multiple csv files
        }
    
        public void readCSV(){
            try {
                BufferedReader buffeReader = new BufferedReader(new FileReader("inputCSV.csv"));
                while ((line = buffeReader.readLine()) != null) {   //get every single line individually in csv file
    
                    rowCount++;
                    if(rowCount == 1){
                        openNewFile();
                    }
    
                    String[] value = line.split(",");   //collect the comma separated values into array
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(value[0]+','+value[1]+','+value[2]+'\n');  //append the array values to stringBuilder with comma separation
                    writer.write(stringBuilder.toString()); //write individual data line into csv file
    
                    if(rowCount == 100){
                        fileCount++;
                        closeWriter();
                    }
                }
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void openNewFile(){
            try {
                writer = new PrintWriter(new File("your_multiple_csv_file_output_path/"+fileCount+".csv")); //create a new csv file
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    
        public void closeWriter(){
            writer.close(); //close a csv file
            rowCount = 0;
        }
    
    
        public void readMultipleCSV(){
            ArrayList<File> xmlFileList = new ArrayList();
            System.out.println(xmlFileList.size());
            xmlFileList.addAll(Arrays.asList(new File("your_multiple_csv_file_output_path").listFiles()));  //add all generated csv files into array list
    
            for (int i = 0; i < xmlFileList.size(); i++) {
                People people = new People();
                ArrayList<Person> list = new ArrayList();
                try {
                    BufferedReader br = new BufferedReader(new FileReader(xmlFileList.get(i).toString()));  //get csv file separately
                    while ((line = br.readLine()) != null) {    //get every single line individually in csv file
                        String[] value = line.split(",");   //collect the comma separated values into array
                        Person person = new Person();
                        person.setId(value[0]); //first element of an array is id
                        person.setFirstName(value[1]);  //second element of an array is firstName
                        person.setLastName(value[2]);   //third element of an array is lastName
                        list.add(person);   //add person object into the list
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                people.setListPerson(list);
                prepareXML(people, i);
            }
        }
    
        public void prepareXML(People people, int csvFileNo){
            //marshaling with java
            try {
    
                JAXBContext jaxbContext = JAXBContext.newInstance(People.class);
                javax.xml.bind.Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
                jaxbMarshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
                jaxbMarshaller.marshal(people, new File("your_xml_file_output_path/output "+(csvFileNo+1)+".xml"));
                jaxbMarshaller.marshal(people, System.out);
    
            } catch (JAXBException e) {
                e.printStackTrace();
            }
        }
    }
    

    inputCSV.csv(没有列名)

    1,yong,mook kim
    2, Alez, Aunritod
    3,yong,mook kim
    .
    .
    .
    

    【讨论】:

    • 嗨 Lakshan,感谢您的回复。实际上我不是在寻找第 1000 条记录。我的 csv 文件有数百万条记录,我想将它拆分并在每次每 1000 或 2000 条记录时一起读取并将它们转换为 XML 并继续到文件末尾
    • 误会见谅,请参考上面修改后的答案
    • 嗨 Lakshan,再次感谢您的帮助。我用我的 CSV 的 150 行测试了这段代码,并试图将它们分成每 100 行。所以它创建了两个 CSV 文件,其中一个有 100 行,另一个有 50 行,但是当它创建 XML 时,第一个 XML 有 100 个人员对象,但第二个有 150 个 .. 我的意思是将它们合并在一起......
    • 你想为 csv 文件中的每一行创建单独的 xml 文件吗?
    • 否,对于每个 csv 文件。我的意思是使用您的代码,它完全可以创建两个 csv 文件,但是当要为这些 CSV 文件中的每一个创建 XML 时,csv1 完全转换为 XML,但 XML2 包含 CSV1+CVS2 ...我的意思是它添加了前面的行也在第二个 XML 中
    猜你喜欢
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2019-08-21
    • 2014-06-09
    • 2011-09-12
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多