-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDownloader.java
More file actions
41 lines (36 loc) · 1.2 KB
/
Downloader.java
File metadata and controls
41 lines (36 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class Downloader {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String downloadPath = "/Users/amit/Documents/FileHandlingTesting/download";
System.out.println("Enter the URL");
Scanner scanner = new Scanner(System.in);
String urlStr = scanner.next();
URL url = new URL(urlStr);
URLConnection connection = url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
String fileName = urlStr.substring(urlStr.lastIndexOf("/"));
System.out.println("File Name is "+fileName);
FileOutputStream fo = new FileOutputStream(downloadPath+fileName);
BufferedOutputStream bs = new BufferedOutputStream(fo);
BufferedInputStream bi = new BufferedInputStream(is);
int singleByte = bi.read();
while(singleByte!=-1){
bs.write(singleByte);
singleByte = bi.read();
}
System.out.println("File Downloaded");
bs.close();
bi.close();
is.close();
fo.close();
}
}