I am trying to write the output of a program to a .txt file but unable to do so.
Basically I am trying to get the whole content of a URL to be written to a file.
The program outputs the content on the console , but fails to write to the file. What am I doing wrong?
Any advise will be great.
Thanks
Code:
import java.net.*;
import java.io.*;
public class MainClass
{
public static void main(String[] args) throws Exception {
URL comed = new URL("https://il.thewattspot.com/login.do?method=showChart");
BufferedReader in = new BufferedReader( new InputStreamReader(comed.openStream()));
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
System.setOut(out);
in.close();
}
}