2017-05-15 83 views
0

我有一个.csv文件包含100k行,我使用mysql来生成该文件。 我想将该文件分割成更小的文件,每个文件包含3k行。 我该如何使用mysql或java?哪个更快? 我在java中找到了解决方案,但它使用缓冲区读取器和缓冲区写入器来处理O(n^2)。使用mysql分割.csv文件

在此先感谢。

+0

请仔细阅读[我可以问什么议题有关(HTTP:// (http://stackoverflow.com/help/how-to-ask) 和[完美的问题](http:// codeblog .jonskeet.uk/2010/08/29/writing-the-perfect-question /) 以及如何创建[Minimal,Complete and Verifiable example](http://stackoverflow.com/help/mcve) – RiggsFolly

+0

这是**最糟糕的是一个O(n)问题。这个所谓的O(n^2)解决方案是什么? –

+0

@KevinAnderson不是读取缓冲区需要O(n)和外部需要O(n)? – RGB

回答

0

用java你可以使用java.I宁愿使用UNIX脚本中,我们使用的是在分割文件下面的例子拆分文件我们

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

     String src="C:\\Users\\sahug\\Downloads\\split\\dumps.txt"; 
     String dest="C:\\Users\\sahug\\Downloads\\split\\"; 
     String fileName="MyFile"; 
     FileInputStream fis = new FileInputStream(str); 
     int size = 20; 
     byte buffer[] = new byte[size]; 

     int count = 0; 
     while (true) { 
      int i = fis.read(buffer, 0, size); 
      if (i == -1) 
       break; 

      String filename = fileName + count; 
      FileOutputStream fos = new FileOutputStream(dest+filename); 
      fos.write(buffer, 0, i); 
      fos.flush(); 
      fos.close(); 

      ++count; 
     } 
    } 
+0

谢谢。 (int size = 20)这是否意味着新文件将有20行?! – RGB

+0

没有,这是我在缓冲区数组中使用的文件的大小。这里20意味着20字节 –

+0

好吧,但我怎么能让每个文件只包含3k?! – RGB