Problem with reading large file Large file can be any plain text or binary file which is huge in size and can not fit in JVM memory at once. For example if a java application allocated with 256 MB memory and it tries to load a file completely which is more or close to that memory in size then it may throw out of memory error. Points to be remembered Never read the whole file at once. Read file line by line or in chunks, like reading few lines from text file or reading few bytes from binary file. Do not store the whole data in memory, like reading all lines and keeping as string. Java has many ways to read the file in chunks or line by line, like BufferedReader, Scanner, BufferedInputStream, NIO API. We will use NIO API to read the file and Java stream to process it. We will also see how to span the processing with multiple threads to make the processing faster. CSV file In this example I am going to read a CSV file which is around 500 MB in size. Sample is as...
Blogs about Java, J2ee, Multithreading, Data structure, Algorithm, Spring framework, Spring boot, Web services and open source technologies