0

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;
APerson
  • 8,140
  • 8
  • 35
  • 49
  • You have `getFileName(url)`. Where does `url` come from? – PM 77-1 May 01 '14 at 01:57
  • Also consider using the System property `user.home` (`System.getProperty("user.home")`) and storing the files within a sub directory within in, rather then cluttering the users desktop :P – MadProgrammer May 01 '14 at 02:08

1 Answers1

0

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)

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • `String locID = "C:\Users\user\Desktop";` `String destFile = locID + File.separator + getFileName(url);` `RandomAccessFile file = new RandomAccessFile(destFile, "rw");` I tried something like this and it works. But, when i try other location, it's not saving inside that directory. Where is the mistake? – user2346133 May 02 '14 at 13:51
  • could it be due to user permissions? – Scary Wombat May 07 '14 at 00:30