Java reading long text file is very slow -
i have text file (xml created xstream) 63000 lines (3.5 mb) long. i'm trying read using buffered reader:
bufferedreader br = new bufferedreader(new filereader(file)); try { string s = ""; string tempstring; int = 0; while ((tempstring = br.readline()) != null) { s = s.concat(tempstring); // s=s+tempstring; = + 1; if (i % 1000 == 0) { system.out.println(integer.tostring(i)); } } br.close(); here can see attempts measure reading speed. , it's low. takes seconds read 1000 lines after 10000 line. i'm doing wrong, can't understand what. in advance help.
@paulgrime right. copying string each time loop reads line. once string gets big (say 10,000 lines big), doing lot of work copying.
try this:
stringbuilder sb = new stringbuilder(); while (...reading lines..){ .... sb.append(tempstring); //should add newline ... } s = sb.tostring(); note: read paul's answer below on why stripping newlines makes bad way read in file. also, mentioned in question comments, xstream provides way read file , if had not, ioutils.tostring(reader) safer way read file.
Comments
Post a Comment