문제

I am reading a large text file from internet into string and I would like to display some sort of progress. Normally I would use ProgressMonitorInputStream but this program is running strictly in command line so this is not an option. Is there any easy way to get some sort of percentage, bytes remaining or any other kind of feedback? This is my code:

URL inUrl = new URL(*URL to be red*);
InputStream inStream=inUrl.openStream();
BufferedReader in=new BufferedReader(new InputStreamReader(inStream,Charset.forName("UTF-8")));
while(in.ready()){

    string=string+in.readLine();

}
in.close();
도움이 되었습니까?

해결책

First get the file size using File.length method. or HttpConnection.getContentLength() if its Internet based resource.

in the while loop the percentage is

while(in.ready()){
    string = string +in.readLine();
    percentage = (string.length() / fileLength) * 100;
}

By the way its not a good practice to use

string + in.readLine()

Use a StringBuffer to this concatenation.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top