且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将文件拆分为多个文件

更新时间:2023-01-24 19:33:48

你先数行数

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

***一次性读取文件,将其放入List(可调整大小),然后使用存储在内存中的行进行拆分.

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.

似乎您听从了我的建议并偶然发现了下一个问题.您可能应该问另一个问题,嗯……这会创建一个包含所有行的缓冲区.

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 是文件中的全局行计数器.这样你每次创建 50 行不同的文件:)

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