java - Scanner's buffer not representative of entire file when newlines enabled -


basically, i'm doing writing file , reading later. few times looking @ buffer, , seeing lines 'cut off,' , getting concerned flushing issue. however, stumbled across this question, states:

so, appears scanner not read entire file @ once...it reads file buffer - means in chunks.

and see reflected in scanner. looking @ buffer size, see 1024 size.

however! writing each entry separate line, passing in message , appending \n before writing. taking \n away results in interesting. when running without newlines, find buffer size has magically increased interesting 5,232, , can see entire contents of file in buffer!

the way make scanner new scanner(new fileinputstream("path.txt")), , inspect using intellij's variable inspection (that's got idea of cutting off from, wasn't able see in file)

essentially, question is: why adding newlines force buffer fixed size , obey rules, , not adding newlines (meaning entire file 1 line) lets buffer whatever size needs be?

i suggest, if want read file, use bufferedreader instead of scanner. see stack overflow post more information.

to answer question: you're right, default scanner buffer size 1024 (as seen here).

your problem larger scanner buffer result of fact scanner reads longest line buffer, if longer default 1024 bytes. removing of \n in file made scanner think there 1 long line, has buffer.

as can see here, buffer size has no effect on efficiency when reading files.

hope


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -