Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You haven't specified to write to a file. If you set the system output redirect, it has to be done before you output.

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks that worked!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •