[Solved] java code to split text file into chunks based on chunk size


That’s because BufferedReader.readLine() reads only a line not the whole file.

I assume that the line break characters \r and \n are not part of the normal content you interested in.

Maybe that helps.

// ...
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

10

solved java code to split text file into chunks based on chunk size