java - Reading with InputStream while using Scanner -
i want read client's inputstream
in 2 different ways:
1: use scanner
on inputstream
read strings.
2: use inputstream
object read file buffer.
at first, want read text, gives me name , size of file, in case: "file:name:...", "file:size:...".
when have information, inputstream
should read file. problem is, try read file in "scanner.hasnextline()
"-loop inputstream
object causes inputstream.read()
method return -1 or eos. have brought problem minimal size have answer me question:
why read-method return -1 here:
if (scanner.hasnextline()) { system.out.println(inputstream.read()); }
as oracle documentation scanner.hasnextline()
states, method does not advance scanner
. means perpetually looking @ first line of file scanner
, while advancing inputstream
. means though scanner
has next line, inputstream
may not. appear walking off end of file inputstream
.
if not advance scanner
along input stream, cannot know scanner
looking @ same location in file. dubious @ best read 1 file simultaneously scanner
, inputstream
anyway.
consider refactoring code use 1 or other.
if read entire contents of file single string
, consider this:
static string readfile(string path, charset encoding) throws ioexception { byte[] encoded = files.readallbytes(paths.get(path)); return new string(encoded, encoding); }
and other methods how create java string contents of file?
Comments
Post a Comment