将我的CSV文件中的每1000行拆分为JAVA格式的XML文件

将我的CSV文件中的每1000行拆分为JAVA格式的XML文件,java,xml,csv,split,marshalling,Java,Xml,Csv,Split,Marshalling,我有一个java程序,可以读取我的CSV文件并将其转换为XML文件,但问题是我的CSV文件有很多行,程序无法将所有行转换为XML,因此我需要读取我的CSV行中的每1000行,并为每行创建一行XML 下面是我的代码,但我不能阅读每1000行 import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.i

我有一个java程序,可以读取我的
CSV
文件并将其转换为
XML
文件,但问题是我的
CSV
文件有很多行,程序无法将所有行转换为
XML
,因此我需要读取我的
CSV
行中的每1000行,并为每行创建一行
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();
    }
    }    
}



导入java.io.BufferedReader;
导入java.io.DataInputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.Scanner;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.JAXBException;
公共类XMLMainApp{
公共静态void main(字符串[]args)引发FileNotFoundException{
UteXmlComunicazione UteXmlComunicazione=新的UteXmlComunicazione();
//读取csv文件并收集所有对象
试一试{
字符串inputfile=“sample.csv”;//源文件名。
double nol=1000.0;//每个输出文件中要拆分和保存的行数。
文件文件=新文件(inputfile);
扫描仪=新扫描仪(文件);
整数计数=0;
while(scanner.hasNextLine())
{  
scanner.nextLine();
计数++;
}  
System.out.println(“文件中的行:“+count”);//显示输入文件中的行数。
双温=(计数/nol);
int temp1=(int)temp;
int nof=0;
if(temp1==temp)
{  
nof=temp1;
}  
其他的
{  
nof=temp1+1;
}  
System.out.println(“要生成的文件数量:”+nof);//显示要生成的文件数量。
FileInputStream fstream=新的FileInputStream(inputfile);
DataInputStream in=新的DataInputStream(fstream);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
弦斯特林;

对于(int j=1;j尝试以下解决方案,此示例与您前面的问题相关。首先,您应该将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;
    }
}
将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();
        }
    }
}

你好,Lakshan,谢谢你的回复。事实上,我不是在寻找记录编号1000。我的csv文件有数百万条记录,我想将其拆分,每次读取1000条或2000条记录,并将其转换为XML,然后继续到文件末尾。对于误解,请参考上面修改的回答Hi Lakshan,再次感谢您的帮助。我用150行CSV测试了这段代码,并尝试将它们拆分为每100行。因此,它创建了两个CSV文件,其中一个有100行,另一个有50行,但当它创建XML时,第一个XML有100人对象,但第二个有150人对象。我的意思是它将它们合并在一起…您想为ev创建单独的XML文件吗csv文件中是否只有一行?不,每个csv文件都有一行。我的意思是,使用您的代码,它会创建两个csv文件,完全可以,但是当您要为每个csv文件创建XML时,csv1会完全转换为XML,但XML2包含csv1+CVS2…我的意思是它会在第二个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();
        }
    }
}
1,yong,mook kim
2, Alez, Aunritod
3,yong,mook kim
.
.
.