Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-10-2012, 11:41 PM   PM User | #1
SunJune
New Coder

 
Join Date: Dec 2010
Posts: 19
Thanks: 5
Thanked 0 Times in 0 Posts
SunJune is an unknown quantity at this point
Writing output to a file

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();
 }

}
SunJune is offline   Reply With Quote
Old 08-11-2012, 12:06 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,661
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You haven't specified to write to a file. If you set the system output redirect, it has to be done before you output.
Fou-Lu is offline   Reply With Quote
Old 08-11-2012, 12:51 AM   PM User | #3
SunJune
New Coder

 
Join Date: Dec 2010
Posts: 19
Thanks: 5
Thanked 0 Times in 0 Posts
SunJune is an unknown quantity at this point
Thanks that worked!
SunJune is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:15 PM.


Advertisement
Log in to turn off these ads.