【问题标题】:java code to split text file into chunks based on chunk sizejava代码根据块大小将文本文件拆分为块
【发布时间】:2014-12-07 02:08:03
【问题描述】:

我需要将给定的文本文件分成大小相等的块并将它们存储到一个数组中。输入是同一文件夹中的一组许多文本文件。我为此使用以下代码:

int inc = 0;
File dir = new File("C:\\Folder");
    File[] files = dir.listFiles();
    for (File f : files) {
        if(f.isFile()) {
            BufferedReader inputStream = null;
            try {
                inputStream = new BufferedReader(new FileReader(f));
                String line;

                while ((line = inputStream.readLine()) != null) {
                    String c[] = splitByLength(line, chunksize);
                    for (int i=0;i<c.length;i++) {
                        chunk[inc] = c[i];
                        inc++;
                    }
                }
            }
            finally {
                if (inputStream != null) {
                    inputStream.close();
                }
            }
        }
    }

public static String[] splitByLength(String s, int chunkSize) {  

    int arraySize = (int) Math.ceil((double) s.length() / chunkSize);  
    String[] returnArray = new String[arraySize];  
    int index = 0;  
    for(int i=0; i<s.length(); i=i+chunkSize) {  
        if(s.length() - i < chunkSize) {  
            returnArray[index++] = s.substring(i);  
        }   
        else {  
            returnArray[index++] = s.substring(i, i+chunkSize);  
        }  
    }
    return returnArray;  
}

这里的块值存储在“块”数组中。但是这里的问题是,由于我使用了 readLine() 命令来解析文本文件,因此只有当块大小小于一行中的字符数时,得到的结果才是正确的。假设每行有 10 个字符,文件中的行数为 5。然后,如果我提供大于 10 的任何值的块大小,它总是将文件分成 10 个块,每个块中的每一行。

例如,考虑一个包含以下内容的文件,

abcdefghij
abcdefghij
abcdefghij
abcdefghij
abcdefghij

如果块大小 = 5 那么,

abcde | fghij | abcde | fghij | abcde | fghij | abcde | fghij | abcde | fghij |

如果块大小 = 10 那么,

abcdefghij | abcdefghij | abcdefghij | abcdefghij | abcdefghij |

如果块大小 > 10 那么我的代码也只提供与以前相同的功能,

abcdefghij | abcdefghij | abcdefghij | abcdefghij | abcdefghij |

我尝试使用 RandomAccessFile 和 FileChannel,但无法获得所需的结果... 谁能帮我解决这个问题?谢谢你。。

【问题讨论】:

  • 如果最后一个块小于你的块大小,将新读取的字符串附加到它的末尾。然后照常继续。

标签: java file chunks chunking


【解决方案1】:

那是因为BufferedReader.readLine() 只读取一行而不是整个文件。

我假设换行符 \r\n 不是您感兴趣的正常内容的一部分。

也许有帮助。

// ...
StringBuilder sb = new StringBuilder(); 
String line;
while ((line = inputStream.readLine()) != null) {
    sb.append(line);

    // if enough content is read, extract the chunk
    while (sb.length() >= chunkSize) {

        String c = sb.substring(0, chunkSize);
        // do something with the string

        // add the remaining content to the next chunk
        sb = new StringBuilder(sb.substring(chunkSize));
    }
}
// thats the last chunk
String c = sb.toString();
// do something with the string

【讨论】:

  • 它适用于小文件,但是当我使用大内容文件时,它显示 java 堆空间错误..
  • 好的,我更新了我的答案。现在它将从文件中读取,直到达到 chunkSize。然后进行拆分并读取下一个块。
  • 我不明白你的代码是如何工作的?你已经在两个地方调用了 splitByLength..
  • splitByLength 不是必需的,我删除了它。
  • 我们需要在while循环内外都做String "c"吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
  • 2017-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
相关资源
最近更新 更多