-2

I need help with a IO java code, I am trying to save java console output [from URL class] to a file in local machine, I am able to get the output need help in saving that file to the local machine

to get the output I did this coding

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

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://www.yahoo.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();
    }
}

Note : I have a another code, in which I can save the output to a file, but unable to co-relate both of the both, I have tried extending, but no progress , here is that class

import java.io.*;
class FileWrite {
    public static void main(String args[]) {
        try{
            // Create file 
            FileWriter fstream = new FileWriter("out.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("Hello Java");
            //Close the output stream
            out.close();
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}
chikuba
  • 4,229
  • 6
  • 43
  • 75
  • 4
    Is that what your code really looks like? Don't you have any indenting at all? Your code is very hard to read. – Greg Hewgill Apr 15 '12 at 07:10
  • 4
    They put Shift keys on a keyboard for a reason. It's so you don't have to SHOUT ALL THE TIME. When you SHOUT, it makes your question harder to read, it's annoying, and it doesn't get you an answer any faster. (Also, your question may be important to you, but so are the questions of others to them. Begging for URGENT or ASAP help just clutters your question. Just ask without the urgency pleas, and someone will help you as soon as they can.) – Ken White Apr 15 '12 at 07:13
  • For the writing part, here's an example: http://stackoverflow.com/a/22074145/3315914 – rpax Feb 28 '14 at 18:40

2 Answers2

2

It's actually almost all there, you just need to know what each line of code does to know how to put it together.

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

public class WriteFromURL {
    public static void main(String[] args) throws Exception {
        try{

            // Block from connection reader
            URL oracle = new URL("http://www.yahoo.com/");
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
            yc.getInputStream()));

            // Block from writer
            // Create file 
            FileWriter fstream = new FileWriter("out.txt");
            BufferedWriter out = new BufferedWriter(fstream);

            // Block from reader w/ small change
            String inputLine;
            while ((inputLine = in.readLine()) != null){
                // Write what you read
                out.write(inputLine);
                out.newLine();
            }
            in.close();

            // Block from writer
            //Close the output stream
            out.close();
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

    }
}
trutheality
  • 23,114
  • 6
  • 54
  • 68
1

You are outputting the text to the console, why not write it to a file?

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));

        FileWriter fstream = new FileWriter("out.txt");
        BufferedWriter out = new BufferedWriter(fstream);

        String inputLine;
        while ((inputLine = in.readLine()) != null){
            out.write(inputLine);
            System.out.println(inputLine);
        }
        out.close();
        in.close();
    }
}

Now two code sections are joined together to do what you want. I didn't run the code. Check it's correctness and also add error handling code.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72