简体   繁体   中英

Download file line by line java

I know this question might sound really basic for most of you. I need to download a large file from server. The first line of this file contains a time tag. I want to download entire file only if my time tag mismatches to that of file. For this I'm using the given code. However, I'm not sure if this actually prevents file from uselessly downloading entire file.

Please help me out !

public String downloadString(String url,String myTime)
{
    try {
           URL url1 = new URL(url);
           URLConnection tc = url1.openConnection();
           tc.setConnectTimeout(timeout);
           tc.setReadTimeout(timeout);
           BufferedReader br = new BufferedReader(new InputStreamReader(tc.getInputStream()));
           StringBuilder sb = new StringBuilder();
           String line;
           while ((line = br.readLine()) != null) {

                    if(line.contains(myTime))
                    {
                        Log.d("TIME CHECK", "Article already updated");
                        break;
                    }
                    sb.append(line+"\n");
                }
                br.close();
                return sb.toString();
        }
    catch(Exception e)
    {
        Log.d("Error","In JSON downloading");
    }

    return null;
}

No, there is no easy way to control exactly to the last byte what will be downloaded. Even at the Java level you are involving a BufferedReader , which will obviously download more than you ask for, buffering it. There are other buffers as well, including at the OS level, which you cannot control. The proper technique to download only new files with HTTP is to use the IfModifiedSince header.

您的代码不会下载整个文件,但由于BufferedReader的默认缓冲区大小为8192,您将至少读取这么多字符。

You can go byte-by-byte or chunk-by-chunk if it is the size

BufferedInputStream in = new BufferedInputStream(url).openStream())
byte data[] = new byte[1024];
int count;
while((count = in.read(data,0,1024)) != -1)
{
    out.write(data, 0, count);
}

Check this question please

How to download and save a file from Internet using Java?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM