JAVA代码将CSV文件拆分为不同的CSV文件,并将单个列数据从父文件提取到子文件

时间:2017-07-11 05:35:28

标签: java csv

我有一个CSV文件,其中包含近10000行数据。我想根据总行数将该文件拆分为10个不同的CSV文件,这样每个文件可以包含1000行数据,顺序第一个文件应该有1-1000行,第二个文件应该有1001-2000行等等上。此外,这10个不同的CSV文件中的每一个都应该只包含父CSV文件的第一列中的数据。我开发的代码将相同的数据(1.e 1-1000行)写入所有10个csv文件。我无法弄清楚代码中的错误。

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

   String inputfile = "C:/Users/Downloads/File.csv";
   BufferedReader br = new BufferedReader(new FileReader(inputfile)); 
   FileWriter fstream1 = new FileWriter("C:/Users/Downloads/FileNumber_"+j+".csv");       
   BufferedWriter out = new BufferedWriter(fstream1);  

   String strLine = null; 

   for (i=i+1;i<=(j*lines);i++) {   //I Have declared i as static int i = 0 and have already calculated lines and files in other part of code

    strLine = br.readLine();   
    if (strLine!= null) { 

        String strar[] = strLine.split(",");
        out.write(strar[0]);   
        if(i!=(j*lines)) {  
            out.newLine(); }  
    }
   }


   out.close();   

4 个答案:

答案 0 :(得分:1)

10个csv文件中每个文件中都有相同行的问题是由于myFunction方法中的下面一行

BufferedReader br = new BufferedReader(new FileReader(inputfile));

使用变量i,j,lines的逻辑非常有效。但每次调用myFunction时,br(输入文件的BufferedReader)都会再次初始化。

因此br.readLine()将从头开始阅读。因此在10个csv文件中每个都有相同的1000行。

希望它有所帮助!

答案 1 :(得分:1)

Use this code 

import java.io.*;  
import java.util.Scanner;  
public class csvfilesplit
{
    public static void main(String[] args) throws IOException {
        int split;      
        Scanner reader = new Scanner(System.in);  // Reading from System.in
        System.out.println("\n Enter The count to split each file :-----------");
        int  s = reader.nextInt();
        File folder = new File("file/");                                //*** Location of your file 
        int filecount = 0;
            for (File fo :
            folder.listFiles()) {
                    if (fo.isFile()) {
                            filecount++;}
                        }
        System.out.println("Total source file count is :-----------------------    "+filecount+"\n");  //*** Total numbr of orginal file in mentioned folder
        String path = folder.getAbsolutePath();
       // System.out.println("location=-----------"+path);
        File[] listOfFiles = folder.listFiles();
        for (int l = 0; l < listOfFiles.length; l++) {
         if (listOfFiles[l].isFile()) {
           System.out.println("File name Is :--------------------------   " + listOfFiles[l].getName());  //*** File name
            BufferedReader bufferedReader = new BufferedReader(new FileReader(path+"/"+listOfFiles[l].getName()));   // Read a souce file
            String input;
            int count = 0;
            while((input = bufferedReader.readLine()) != null)
            {
             count++;
            }  
     System.out.println("File total rows count is :--------------   "+count);   //*** Number of row count in the file
     split= count/s;

     int n = split,z=0;
     if(n!=z)
     {
      System.out.println("Each splitted file line count is :------   "+split+"\n"); //*** After splitted  file have the rows count
      FileInputStream fstream = new FileInputStream(path+"/"+listOfFiles[l].getName()); DataInputStream in = new DataInputStream(fstream);  
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine;  
      for (int j=1;j<=s;j++)  
       {  
        File dir = new File(path+"/"+"CSV_DATA_"+j);
        dir.mkdir(); 
        File filefolder = new File(path+"/"+"CSV_DATA_"+j);
        String folderpath = filefolder.getAbsolutePath();         
        //********Destination File Location******
        FileWriter fstream1 = new FileWriter(folderpath+"/"+listOfFiles[l].getName()+".csv");   //*** Splitted files  and file format(.txt/csv.....)
        BufferedWriter out = new BufferedWriter(fstream1);   
        for (int i=1;i<=n;i++)  
         {  
         strLine = br.readLine();   
         if (strLine!= null)  
           {  
           out.write(strLine);   
            if(i!=n)  
             {  
             out.newLine();  
             } 
            }     
          }  
          out.close(); 
              } 
  in.close();  
    }  
    else
        {// Below N number of row in this file
            System.out.println("\n******************************* Mentioned this file have below - "+s+" rows   ******************************   "+listOfFiles[l].getName()+" \n");}
       }
}
System.out.println("\n Splitted_CSV_files stored location is :     "+path);
 }
}

答案 2 :(得分:0)

请找到以下代码: -

public static void main(String[] args) throws IOException {
           //first read the file
           String inputfile = "C:/Users/bohrahas/Desktop/SampleCSVFile.csv";
           BufferedReader br = new BufferedReader(new FileReader(inputfile)); 
           //create thje first file which will have 1000 lines
           File file = new File("C:/Users/bohrahas/Desktop/FileNumber_"+1+".csv");
            FileWriter fstream1 = new FileWriter(file);
            BufferedWriter out = new BufferedWriter(fstream1);  
               String line="";
               //count the number of line
               int count=1;
               int file_Number=2;
               while ((line = br.readLine()) != null) 
               {
                   //if the line is divided by 1000 then create a new file with file count
                   if(count % 1000 == 0)
                   {
                       File newFile = new File("C:/Users/bohrahas/Desktop/FileNumber_"+file_Number+".csv");
                       fstream1 = new FileWriter(newFile);
                       file_Number++;
                       out = new BufferedWriter(fstream1); 
                   }
                    if(line.indexOf(",")!=-1)
                    line=line.substring(0, line.indexOf(","));
                    out.write(line);
                    out.newLine();
                   count++;
               }

}

逻辑: -

  1. 您不必为每个循环读取父文件。只需加载一次,即创建一个对象,然后处理父文件。

  2. 在读取父线的每一行时,只需删除第一列以外的列。

  3. 直到第一个&#34;,&#34;始终是第一列。所以在&#39;&#39;&#39;,&#34;
  4. 之后删除字符串
  5. 如果遍历的行数完全除以1000,即2000,3000,4000 ......等。创建一个新文件并为其创建一个BufferWriter。

答案 3 :(得分:-2)

你的逻辑非常糟糕。我为你重写了整个代码,

import java.io.*;  
import java.util.Scanner;  


public class FileSplit {  

public static void myFunction(int lines, int files) throws FileNotFoundException, IOException{

    String inputfile = "file.csv";
    BufferedReader br = new BufferedReader(new FileReader(inputfile)); //reader for input file intitialized only once
    String strLine = null; 
    for (int i=1;i<=files;i++) { 
        FileWriter fstream1 = new FileWriter("FileNumber_"+i+".csv"); //creating a new file writer.       
        BufferedWriter out = new BufferedWriter(fstream1);  
        for(int j=0;j<lines;j++){   //iterating the reader to read only the first few lines of the csv as defined earlier
             strLine = br.readLine();   
            if (strLine!= null) { 
               String strar[] = strLine.split(",");
               out.write(strar[0]);   //acquring the first column
               out.newLine();   
            } 
        }
        out.close(); 
        }  
   }

public static void main(String args[])  
{  
 try{  
     int lines = 2;  //set this to whatever number of lines you need in each file
     int count = 0;
     String inputfile = "file.csv";
     File file = new File(inputfile);  
     Scanner scanner = new Scanner(file);  
     while (scanner.hasNextLine()) {  //counting the lines in the input file
        scanner.nextLine();  
        count++;  
      }  
     System.out.println(count);
     int files=0;  
     if((count%lines)==0){  
        files= (count/lines);  
      }  
      else{  
         files=(count/lines)+1;  
      }   
      System.out.println(files); //number of files that shall eb created

      myFunction(lines,files);
 }

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

}