java - How to read a large file using Nio2 -


i trying read text file has around 3 lakh lines of now.

how reading?

i reading using java.io.bufferedreader

here small code snippet represents approach.

int linenumber = 1; bufferedreader br = null; string currentline = null; br = new bufferedreader(new filereader(f));//here f file name read, have passed while ((cuurentline = br.readline()) != null) {   //here have written logic processing after reading 1000 lines   //line number = 1001 start processing, reads next 1000 lines, each line put in list collection   //after reaching 1001 line clearing list , continuing loop } 

i have tried using nio2 following case

br = files.newbufferedreader(paths.get(inputfilename), standardcharsets.utf_16); 

it resulted in follwoing exception

exception :exception in thread "main" java.lang.outofmemoryerror: java heap space     @ java.util.arrays.copyof(unknown source)     @ java.lang.abstractstringbuilder.expandcapacity(unknown source)     @ java.lang.abstractstringbuilder.ensurecapacityinternal(unknown source)     @ java.lang.abstractstringbuilder.append(unknown source)     @ java.lang.stringbuffer.append(unknown source)     @ java.io.bufferedreader.readline(unknown source)     @ java.io.bufferedreader.readline(unknown source)     @ texttoexcelmerger.readfilelinebyline(texttoexcelmerger.java:66)     @ texttoexcelmerger.main(texttoexcelmerger.java:255) 

firstly, approach right?

are there efficient , fast approaches in nio2, apache fileutils or other api reading file faster, improves file reading process faster. can read set of lines first 1000 like
br.readfirst(1000);,
without reading line line or iterating in logic?

any approach reads entire files memory doomed failure. sooner or later file exceed available memory, , program cease function , have redesigned completely. not failure mode, there nothing user can in interim. @ point mow. pretty ambitious try file hundreds of thousands of lines. rethink , process line @ time. or use database.

nb don't kid yourself. you're using java.io read file. nio2 component here minimal. not need @ all.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -