将我的CSV文件每1000行拆分成一个XML文件,使用JAVA。

huangapple go评论83阅读模式
英文:

Spliting each 1000 rows of my CSV file to a XML File in JAVA

问题

我有一个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();        
        
        // 读取CSV文件并收集所有对象
        try {
        	
        	String inputfile = "sample.csv"; // 源文件名。  
        	  double nol = 1000.0; // 每个输出文件中要拆分和保存的行数。  
        	  File file = new File(inputfile);  
        	  Scanner scanner = new Scanner(file);  
        	  int count = 0;  
        	  while (scanner.hasNextLine())   
        	  {  
        	   scanner.nextLine();  
        	   count++;  
        	  }  
        	  System.out.println("文件中的行数:" + count);     // 显示输入文件中的行数。  

        	  double temp = (count/nol);  
        	  int temp1=(int)temp;  
        	  int nof=0;  
        	  if(temp1==temp)  
        	  {  
        	   nof=temp1;  
        	  }  
        	  else  
        	  {  
        	   nof=temp1+1;  
        	  }  
        	  System.out.println("要生成的文件数:"+nof); // 显示要生成的文件数。  
        	
        	  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) {    // 逐行获取CSV文件中的每一行
                String[] value = line.split(";");   // 将逗号分隔的值收集到数组中
                datiCliente datiCliente = new datiCliente();            
                datiCliente.setcfPiva(value[0]);
               ... // 为每个字段分配相关节点。
            
            }

        UteXmlComunicazione.setdatiFornitoraClienteList(listForCliente);
      
        for (int i=1;i<=nol;i++)  
        {  
        // 使用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("文件未找到");
        }
        }
        }

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

当我运行此代码时,它创建了180个文件(这是正确的),但在每个文件中,我有所有的行,而不仅仅是1000行... 我无法打开它。有人可以给我提供一个解决方案吗?

英文:

I have a java program that will read my CSV file and convert it to XML file, but the issue is that my CSV file has lots of rows and the program can not convert all to XML, so I need to read each 1000 of my csv rows and create a XMl for each of them

Below is my code but I can not read each 1000 rows...


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 = &quot;sample.csv&quot;; //  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(&quot;Lines in the file: &quot; + 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(&quot;No. of files to be generated :&quot;+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&lt;=nof;j++)  
{  
while ((line = br.readLine()) != null) {    //get every single line individually in csv file
String[] value = line.split(&quot;;&quot;);   //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&lt;=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(&quot;C:/NewFolder/output&quot;+j+&quot;.xml&quot;));
jaxbMarshaller.marshal(UteXmlComunicazione, System.out);
} 
catch (JAXBException e) {
e.printStackTrace();
System.out.println(&quot;File is not found&quot;);
}
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}    
}

When I run this code, it creates 180 files (that is correct) but in each of them I have all my rows, not just 1000 rows... and I can not open it.
Can anyone suggest me a solution?

答案1

得分: 1

尝试以下解决方案这个示例与您之前的问题相关首先您应该将 CSV 文件拆分成多个包含预期行数的 CSV 文件然后逐个获取准备好的 CSV 文件并创建 XML 文件

People.java

```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();    // 将 CSV 文件拆分成多个 CSV 文件
        marshaller.readMultipleCSV(); // 使用多个 CSV 文件创建 XML 文件
    }

    // ...(下面的方法同样进行翻译,但不再重复展示)
}

inputCSV.csv(没有列名的文件)

1,yong,mook kim
2, Alez, Aunritod
3,yong,mook kim
.
.
.
英文:

try with following solution, this example is related with your previous question. firstly you should split csv file into multiple csv files including with expected row counts. then, get prepared scv file individually and create xml files

People.java

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

Person.java

@XmlRootElement(name=&quot;person&quot;)
@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;
}
}

split csv file into multiple csv files and create xml file separately,

public class Marshaller {
int rowCount;
int fileCount = 1;
PrintWriter writer;
int lineCount;
int index;
String line;
ArrayList&lt;Person&gt; 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(&quot;inputCSV.csv&quot;));
while ((line = buffeReader.readLine()) != null) {	//get every single line individually in csv file
rowCount++;
if(rowCount == 1){
openNewFile();
}
String[] value = line.split(&quot;,&quot;);	//collect the comma separated values into array
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(value[0]+&#39;,&#39;+value[1]+&#39;,&#39;+value[2]+&#39;\n&#39;);	//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(&quot;your_multiple_csv_file_output_path/&quot;+fileCount+&quot;.csv&quot;));	//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&lt;File&gt; xmlFileList = new ArrayList();
System.out.println(xmlFileList.size());
xmlFileList.addAll(Arrays.asList(new File(&quot;your_multiple_csv_file_output_path&quot;).listFiles()));	//add all generated csv files into array list
for (int i = 0; i &lt; xmlFileList.size(); i++) {
People people = new People();
ArrayList&lt;Person&gt; 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(&quot;,&quot;);	//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(&quot;your_xml_file_output_path/output &quot;+(csvFileNo+1)+&quot;.xml&quot;));
jaxbMarshaller.marshal(people, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}

inputCSV.csv (without columns' names)

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

huangapple
  • 本文由 发表于 2020年5月5日 20:16:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/61612923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定