Split file into multiple files

孤者浪人 提交于 2019-12-02 11:24:14

you count the lines first

while((line = br.readLine()) != null){
                    sourcesize++; }

and then you're at the end of the file: you read nothing

for (int i=1;i<=numSplits;i++)  {  
                while((line = br.readLine()) != null){

You have to seek back to the start of the file before reading again.

But that's a waste of time & power because you'll read the file twice

It's better to read the file once and for all, put it in a List<String> (resizable), and proceed with your split using the lines stored in memory.

EDIT: seems that you followed my advice and stumbled on the next issue. You should have maybe asked another question, well... this creates a buffer with all the lines.

for (String value : list) {
                builder.append("/n"+value);
            }

You have to use indexes on the list to build small files.

for (int k=0;k<numSplits;k++) {  
      builder.append("/n"+list[current_line++]);

current_line being the global line counter in your file. That way you create files of 50 different lines each time :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!