java - BufferedReader always resets to last character in a file -
i having trouble reseting bufferedreader random point in reading txt file. example input text in file is: number = 10;
. last part of code system.out.println((char)c);
print out ;
instead of 1
.
doing wrong in simple example?
code:
string filepath = "data.txt"; bufferedreader br = new bufferedreader(new filereader(filepath)); string line = ""; int c = 0; while ((c = br.read()) != -1) { if((char)c == '1') { br.mark(1000); } system.out.print((char)c); } br.reset(); c = br.read(); system.out.println((char)c);
from using code , dummy .txt
file made using number=10;
line. c = br.read()
returns 0
.
the java.io.bufferedreader.mark(int) method marks current position in stream. invoking reset() reposition stream point, here.
so prints character after character have assigned mark.
if wish print rest of line use:
string line = br.readline();
if want 1
reason, modify code such:
c = br.read()+1;
Comments
Post a Comment