This must work from the command line. For this phase, your program will accept t
ID: 3598752 • Letter: T
Question
This must work from the command line. For this phase, your program will accept two file names from the command line; the first is the name of the input file and the second is the name of the output file. . Now, for the details: For this phase, your input file entered will be a list of URLs that you will provide. The lines of input in the file should be of webpages that end in a mix of .htm (or .html), .jpg (or .jpeg), .txt and .pdf file endings. Output the name of the URL and all of its info about the URL to an output file (whose name will be the second parameter on the command line you pass to java; the first is name of the input file above). The remaining processing will depend on the file ending that the URL points to (ie that you will be reading from). If the URL is a .html or .htm or .txt file, then read all the lines of the file and save it to a separate file on your system with the Same Name as the URL file name (ie last portion of actual URL). Then, output the number of lines read in and the name of the file to the output file (again this is referred to by the second parameter on the command line). If the URL is an image file, .jpeg or .jpg or .gif, then save the image file to your computer with the Same Name as the URL file name. Then, output the name of the file to the output file. If the URL is .pdf then save the file to your computer with the Same Name as the URL file name and output the name of the file to the output file. In the case of .pdf you will probably need to copy byte by byte instead of line by line because parts of .pdf files are stored in binary encodings and not ascii.
Explanation / Answer
Given below is the Java code for the question. If using eclipse, place your input file outside of the src folder. It should be placed directly under the project folder outside src. Otherwise the input file will not be found. In case of any issues running the program, post a comment , I will help. If the program helped, please don't forget to rate it. Thank you.
To indent the code, select code in file by pressing Ctrl+A and then Ctrl+i
In order to run the program, you need to pass 2 command line arguments - input and output filenames. The input file should contain a list of urls to download. A sample is given below. The output is written to output file. To see the downloaded files you will need to refresh the project in eclipse.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class DownloadURL {
private static String extractFileName(String path)
{
int index = path.lastIndexOf("/");
if(index == -1)
return path.trim();
else
return path.substring(index+1);
}
private static void downloadBytes(URL url, String filename, PrintWriter outfile)
{
byte[] bytes = new byte[1000];
int len;
try {
InputStream is = url.openStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filename)));
BufferedInputStream bis = new BufferedInputStream(is);
while((len = bis.read(bytes, 0, 1000)) != -1)
{
bos.write(bytes, 0, len);
}
bis.close();
bos.close();
outfile.println("File: " +filename);
System.out.println("Downloaded " + filename);
} catch (IOException e) {
outfile.println("File: " + filename + " " +e.getMessage());
System.out.println("Error downloading " + filename);
}
}
private static void downloadText(URL url, String filename, PrintWriter outFile)
{
InputStream is;
try {
is = url.openStream();
Scanner infile = new Scanner(is);
PrintWriter file1 = new PrintWriter(new File(filename));
int lines = 0;
while(infile.hasNextLine())
{
lines++;
file1.write(infile.nextLine() + " ");
}
infile.close();
file1.close();
outFile.println("File: " + filename + ", No. of lines: " + lines);
System.out.println("Downloaded " + filename);
} catch (IOException e) {
outFile.println("File: " + filename + e.getMessage());
System.out.println("Error downloading " + filename);
}
}
public static void main(String[] args) {
String urlPath;
if(args.length != 2)
{
System.out.println("Usage: java DownloadURL <inputfile> <outputfile>");
System.out.println("<inputfile> is the file containing a list of URLs to download");
System.out.println("<outputfile> is the output file");
return;
}
Scanner infile;
try {
infile = new Scanner(new File(args[0]));
PrintWriter outFile = new PrintWriter(new File(args[1]));
while(infile.hasNextLine())
{
urlPath = infile.nextLine();
try {
URL url = new URL(urlPath);
String filename = extractFileName(url.getFile());
int idx = filename.indexOf('.');
String filetype = filename.substring(idx+1).toLowerCase();
if(filetype.equals("htm") || filetype.equals("html") || filetype.equals("txt"))
downloadText(url, filename, outFile);
else
downloadBytes(url, filename, outFile);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
}
infile.close();
outFile.close();
} catch (FileNotFoundException e1) {
System.out.println(e1.getMessage());
}
}
}
sample input file: urls.txt
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
https://i.pinimg.com/originals/3a/b2/41/3ab241796a48a58a318bccb9bbb5c131.jpg
output
Downloaded Pattern.html
Downloaded 3ab241796a48a58a318bccb9bbb5c131.jpg
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.