Pergunta

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();
Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top