There is a compressed file, first I need to decompress it, then read the contents of the line and process each line of data by splitting the two fields and using one of them as the key, then encrypt another field. Some code is as follows:
try (GZIPInputStream stream = new GZIPInputStream(new ByteArrayInputStream(event.getBody()));
BufferedReader br = new BufferedReader(new InputStreamReader(stream))) {
String line;
StringBuilder builder = new StringBuilder();
while ((line = br.readLine()) != null) {
builder.append(line);
this.handleLine(builder);
builder.setLength(0);
builder.trimToSize();
}
} catch (Exception e) {
// ignore
}
- Each compressed package has about three million rows, so how to handle strings efficiently in the loop is the key to the performance of the entire program.
- Is it correct to use
StringBuilderlike this? - The format of each line of data is as follows :
aaa|bbb|ccc|ddd|eee|fff|ggg|hhh.
What I want to know is how to correctly use String and StringBuilder in this extremely large amount of data loop.
handleLineactually manipulate theStringBuildercontent in some way (i.e. change it)? Because if it doesn't then the entirebuilderis pointless, aslinewill have the same content.String#splite()method is too inefficient. I customize the method of splitting the field by index and replace it withStringBuilder#replace().builder.trimToSize();It causes multiple unnecessary reallocations.