We have a requirement where we will be getting the files of sizes varying from 1 to 4 GBs (might increase in future). We need to sort them based on certain criteria and then perform some operations on the records. What is the optimal approach to accomplish this.
Ah ok, so it sounds like you want to parse the file line by line without pulling the whole thing into memory. Something like this would give you a Stream where each item is a line from the file, then you can consume it as you wish
Won't you overwhelm the available heap memory with a 4GB file? Not it you process one element at a time, as Tim suggested.
If you have a database, let the database sort the output for you; databases are very good at handling large datasets like that.