Hello all, I am looking for some help with some hw I have in a computer class. We are doing a little Java but I am pretty new with the whole language so am having a tough time. Any help would be greatly appreciated!
I have been given these rules to follow-
iStream is an instance variable that is an InputStream object.
openInput is a method that takes in a String that is the url address to an input file somewhere on the web. Then, you must use that String to create a new URL object, and create a URLConnection object by opening a connection to the URL object. Finally, get an InputStream from the URLConnection object, and use it to set the value of iStream. All exceptions must be caught.
download is a method that takes in a String that is the directory path/filename of the local file where the information from the web should be written. This method should use a Scanner object to read information from the InputStream iStream and copy it line-by-line into the local file using a FileWriter object. This method must return true if the copy was successful, and return false if the copy was unsuccessful (i.e., file does not exist). All exceptions must be caught.
So far I have this-
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class WebReader_A7part2 { InputStream iStream; Private void openInput(String urlAddress) Private URL myURL = new URL(urlAddress); URLConnection myURLConnection = myURL.openConnection(); myURLConnection.connect(); BufferedReader br = new BufferedReader(new InputStreamReader( myURLconnection.getInputStream())); ObjectInputStream ois = new ObjectInputStream(myURLConnection); String inputLine; while ((inputLine = br.readLine()) != null) System.out.println(inputLine); br.close(); } boolean download(String filePath) Private void Scanner sc = new Scanner(iStream); boolean copyContents(File A, File B){ try{ if(A.exists() & B.exists()){ InputStream in = new FileInputStream(A); OutputStream out = new FileOutputStream(B);
I am having issues using a scanner object to read from the inputstream iStream and copy it line by line using a filewriter object. Any help!?