It's not a secret anymore!
The moose likes Beginning Java and the fly likes save file into directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "save file into directory" Watch "save file into directory" New topic

save file into directory

ernie bert

Joined: Apr 13, 2005
Posts: 4
hi all,
i need a method that downloads a file from the internet and saves it into a directory which has to be created if it doesn't exist. how can i do this? i figured out something like this:

public String getImage() {
String picurl = "";
URL neturl = new URL("http://"+picurl/");
InputStream in = neturl.openConnection().getInputStream();
String localurl = "C:/Cache/"+picurl;
File picture = new File(localurl);
OutputStream out = new FileOutputStream(localurl);
byte[] buffer = new byte[1024];
for (int n;(n = != -1; out.write(buffer, 0, n));
return result; //local path of file = "C:/Cache/"

but that doesn't seem to be the solution... how do i save the file into a (new) directory and return the path of the file as a String? can anybody help?
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
you will find all the information you need here.
Next time you post code use the ubb code tag, there is a button underneath the text input area to make this easy to do. The code tags mantain your indentations making your code easier to read.
[ April 13, 2005: Message edited by: Nigel Browne ]
I agree. Here's the link:
subject: save file into directory
It's not a secret anymore!