0

I have an assingment I have to do so please do not post the code, but can someone explain to me logically how it is possible to save a URL source code as a text file?

So far I have this code from the Oracle docs:

    import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://www.oracle.com/");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

So this is basically showing me the entire source code in the console, but I want to turn it into a text file which I can then analyze to find trends in the data.

Jon Heller
  • 34,999
  • 6
  • 74
  • 132
king
  • 1,304
  • 3
  • 23
  • 43
  • 2
    This answer should help: http://stackoverflow.com/questions/2885173/java-how-to-create-and-write-to-a-file (creating and writing to files in java) – Luigi Apr 14 '14 at 03:03
  • i tried to do 3 different solutions on there.. god i am getting so angry i can't figure this out. is there some video tutorial that shows the syntax of what is supposed to be where? i don't know the right syntax to do anything – king Apr 14 '14 at 03:52
  • like am I supposed to put the text file in place of where the System.out.println is? That's what I tried doing, but to avail. I tried making a different class, but then in the seperate class, it doesn't know the variable I am talking about because the variable is in my main class – king Apr 14 '14 at 03:53
  • @johnc. First comment already quote you a question with an extremely easy way to write to file. If you can't even understand what's going on that question, I believe you need to go grab a Java beginner book and look into the IO section. – Adrian Shum Apr 14 '14 at 04:21
  • Stop getting angry, and stop saying meaningless things like 'to no avail', and start telling us *what specific problem* you are having with the solution provided. NB you aren't saving the 'URL Object', you are saving the content of a resource located by that URL. – user207421 Apr 14 '14 at 04:44

1 Answers1

0

I think below code will help you

public static void main(String[] args) throws IOException{
    downloadFunction("http://google.com.vn",
            "/tmp/google.html", "someHeaderValue");
}

private static void downloadFunction(String url, String outPut, String headerValue) throws IOException{
    URL website = new URL(url);
    URLConnection connection = website.openConnection();
    connection.setRequestProperty("headerKey", headerValue);
    InputStream response = connection.getInputStream();
    ReadableByteChannel rbc = Channels.newChannel(response);
    FileOutputStream fos = new FileOutputStream(outPut);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
Bui Anh Tuan
  • 910
  • 6
  • 12