java - What are the advantages and disadvantages of reading an entire file into a single String as opposed to reading it line by line? -


specifically, end goal store every comma separated word file in list<string> , wondering approach should take.

approach 1:

string filecontents = new scanner(new file("filepath")).usedelimiter("\\z").next(); list<string> list = arrays.aslist(filecontents.split("\\s*,\\s*")); 

approach 2:

scanner s = new scanner(new file("filepath")).usedelimiter(","); list<string> list = new arraylist<>(); while (s.hasnext()){     list.add(s.next()); } s.close(); 

approach #1 read entire file memory. has couple of performance-related issues:

  • if file big uses lot of memory.
  • because of way character's need accumulated scanner.next() call, characters may need copied 2 or 3 times.
  • there other inefficiencies due fact using general pattern matching engine specific purpose.

approach #3 (which approach #1 file reading done better) addresses lot of efficiency issues, still hold entire file contents in memory.

approach #2 best memory usage perspective because don't hold entire file contents single string or buffer1. performance likely best because (my intuition says) approach avoids @ least 1 copy of characters.

however, if matters, should benchmark alternatives, bearing in mind 2 things:

  • "premature optimization" wasted effort. (or put another, chances performance of part of code doesn't matter. performance bottleneck somewhere else.)
  • there lot of pitfalls writing java benchmarks can lead bogus performance measures , incorrect conclusions.

the other thing note trying (create list of "words" in order) not scale. large enough input file, application run out of heap space. if anticipate running on input files larger 100mb or so, may start become concern.

the solution may convert processing more "stream" based ... don't need have list of words in memory.

this same problem problem approach #1.


1 - unless file small , fits buffer ... , whole question largely moot.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -