java - The structure of txt file code is reading from -
can please explain why there have 3 empty rows in between sections of text (marked number) in txt file code reading ?
i have code reading lines txt file. structure of file follows:
1
r
line of text
line of text
line of text
line of text
line of text
2
a
line of text
line of text
line of text
line of text
line of text
a number mark m using identify section read. when section identified code reads consecutive lines of text , returned values assigned string variables. works , if there 3 empty rows separating each section of text source txt file. d understand why ? thank much
here's snippet of code:
inpstream = getclass().getresourceasstream("/resources/myfile.txt"); try (bufferedreader reader = new bufferedreader(new inputstreamreader(inpstream,"utf-16be"))){ do{ num = integer.tostring(a); line = reader.readline(); if(line.equals(num)){ = reader.readline(); = reader.readline(); sat = reader.readline(); pow = reader.readline(); satno = reader.readline(); cash = reader.readline(); break; } } while(!line.equals(num) && (line = reader.readline()) != null ); } catch(ioexception e){}
it number , order of calls readline().
your code reads , checks every other line match. 3 blank lines not important, it's it's number since first index number. work one, 5 etc.
the double read occurs once @ beginning of each iteration, , once in condition.
line = reader.readline(); and
while(... && (line = reader.readline()) != null thereby skipping 2 lines each iteration.
i recommend either moving first read out of loop, or change while-loop:
string line; while ((line = reader.readline() != null) { if (line.equals(num){ ... break; } } this reads each line, checking against num each line.
defining num can moved out of loop, not change inside loop. giving full example of:
inputstream inpstream = getclass().getresourceasstream("/resources/myfile.txt"); try (bufferedreader reader = new bufferedreader(new inputstreamreader(inpstream, "utf-8"))) { num = integer.tostring(a); string line; while ((line = reader.readline()) != null) { if (line.equals(num)) { = reader.readline(); = reader.readline(); sat = reader.readline(); pow = reader.readline(); satno = reader.readline(); cash = reader.readline(); break; } } } catch (ioexception e) { } note: example uses utf-8.
Comments
Post a Comment