简体   繁体   中英

Where to add the directory to save the downloaded files? Java

I have a program which will download files from a specific URL and save them inside the default directory where .java files are stored. However, I want to set a specific location to store the downloaded files.

String locID = "C:\Users\user\Desktop";

This is the directory location I want to insert in the code below. Where in the code should I insert the path locID ?

RandomAccessFile file = new RandomAccessFile(getFileName(url), "rw");
file.seek(downloaded);

InputStream stream = connection.getInputStream();

while (status == DOWNLOADING) {
    byte buffer[];
    if (size - downloaded > MAX_BUFFER_SIZE) {
      buffer = new byte[MAX_BUFFER_SIZE];
    } else {
      buffer = new byte[size - downloaded];
    }

    int read = stream.read(buffer);
    if (read == -1)
    break;

file.write(buffer, 0, read);
downloaded = downloaded + read;

The constructor for the RandomAccessFile can take a File or a String, if you are using a String simple prepend with the directory Location.

If you are using a File, this can also be constructed with a directory location, see http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.lang.String)

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