Forward slash always works, also on Windows.
See "forward-slash-or-backslash"
And backward slash will definitely not work on URLs.
After your response on Abra I understand better what you want to do.
You need to open the URL as a inputstream and create a new outputstream wchich points to your local file.
File understands http layout so you can use it to get the last part of the url which contains the name of the file (see variable f):
File also has a constructor with 2 arguments: path + filename. If you use this one you don't need to bother forward or backward slash problems.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class InternetReader {
private static void copyInputStreamToOutputstream(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
public static void main(String[] args) throws IOException {
File f = new File("http://google.be/test.jpg");
System.out.println(f.getName());
File localPath = new File("/cdn/opt");
File localDestination = new File(localPath, f.getName());
URL remoteURL = new URL("http://google.be/test.jpg");
try (InputStream is = remoteURL.openStream(); OutputStream os = new FileOutputStream(localDestination)) {
copyInputStreamToOutputstream(is, os);
}
}
}