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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Java - Output to file

    This is an assignment, so I'm just looking for a hint.
    Hopefully someone can help, it seems like it should be easy.

    This is the original code:

    Code:
    import java.io.*;
    import java.net.*;
    
    public class scrapeSite
    {
       public static void main( String[] args )
       {
          try
          {
             String line;
             BufferedReader br;
             BufferedWriter bw;
    
             // create a connection to 'www.yahoo.com' on port 80
             Socket s = new Socket( "www.yahoo.com", 80 );
    
             // create the reader and writer objects
             br = new BufferedReader( new InputStreamReader( s.getInputStream() ));
             bw = new BufferedWriter( new OutputStreamWriter( s.getOutputStream() ));
    
             // request the 'root' page
             bw.write( "GET / HTTP/1.0\n\n" );
             bw.flush();
    
             // while more lines, output to the standard output stream
             while( (line = br.readLine()) != null )
             {
                System.out.println( line );
             }
          }
          catch( IOException e )  // catch any errors
          {
             System.out.println( "There was an IOException error!" );
          }
       }
    }
    My assignment is to simply output the result to a text file. After looking through some books and other tutorials, I've come up with this

    Code:
    import java.io.*;
    import java.net.*;
    
    public class scrapeSite
    {
        public static FileOutputStream Output;
        public static PrintStream file;
        public static String line;
    
       public static void main( String[] args )
       {
          try
          {
             BufferedReader br;
             BufferedWriter bw;
    
             // create a connection to 'www.yahoo.com' on port 80
             Socket s = new Socket( "www.yahoo.com", 80 );
    
             // create the reader and writer objects
             br = new BufferedReader( new InputStreamReader( s.getInputStream() ));
             bw = new BufferedWriter( new OutputStreamWriter( s.getOutputStream() ));
    
             // request the 'root' page
             bw.write( "GET / HTTP/1.0\n\n" );
             bw.flush();
    
             // while more lines, output to the standard output stream
             while( (line = br.readLine()) != null )
             {
                //System.out.println( line );            
                Output = new FileOutputStream("myfile.txt");
                // Connect print stream to the output stream
                file = new PrintStream(Output);
                file.println (line);
             }
          }
          catch( IOException e )  // catch any errors
          {
             System.out.println( "There was an IOException error!" );
          }
       }
    }
    It almost works, except only the last line gets writtent to the txt file.
    Am I on the right track?

    Thanks

    ~YC

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Take a look at your while loop and what is going to happen each time that loop is executed. You are going to create a new file output and print stream object with each iteration of the loop. Is that enough of a hint?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Spookster
    Take a look at your while loop and what is going to happen each time that loop is executed. You are going to create a new file output and print stream object with each iteration of the loop. Is that enough of a hint?
    I see what you mean....each time its just going to print the current line, instead of appending it to the end.

    I've tried to move the file/Output stuff outside the while but that doesn't work.

    I tried this
    Code:
    import java.io.*;
    import java.net.*;
    
    public class scrapeSite
    {
        public static FileOutputStream Output;
        public static PrintStream file;
        public static String line;
    
       public static void main( String[] args )
       {
          try
          {
             BufferedReader br;
             BufferedWriter bw;
    
             // create a connection to 'www.yahoo.com' on port 80
             Socket s = new Socket( "www.yahoo.com", 80 );
    
             // create the reader and writer objects
             br = new BufferedReader( new InputStreamReader( s.getInputStream() ));
             bw = new BufferedWriter( new OutputStreamWriter( s.getOutputStream() ));
    
             // request the 'root' page
             bw.write( "GET / HTTP/1.0\n\n" );
             bw.flush();
    
             // while more lines, output to the standard output stream
             while( (line = br.readLine()) != null )
             {
                Output = new FileOutputStream("myfile.txt");
             }
          }
          catch( IOException e )  // catch any errors
          {
             System.out.println( "There was an IOException error!" );
          }
          file = new PrintStream(Output);
          file.println(line);
       }
    }
    but that doesn't work either. It runs but give NullPointerExceptions and it prints null in myfile.txt

    I don't know how to break out of the loop, or move the code out of the loop
    I'm looking at these sites (1,2) but I cant get this thing to run.
    Any more hints?

    ~YC

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Ok I guess my hint wasn't good enough.

    Ok here:

    Output = new FileOutputStream("myfile.txt");

    You have this inside the while loop. With each iteration of the loop you were creating a new instance of that file and essentially overwriting what was written to it the previous iteration. That is why your original code was only writing the last line to the text file.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried removing the while loop, but now it doesn't print anything to the file
    Code:
    public class scrapeSite
    {
        public static FileOutputStream Output;
        public static PrintStream file;
        public static String line;
    
       public static void main( String[] args )
       {
          try
          {
             BufferedReader br;
             BufferedWriter bw;
             Socket s = new Socket( "www.yahoo.com", 80 );
             br = new BufferedReader( new InputStreamReader( s.getInputStream() ));
             bw = new BufferedWriter( new OutputStreamWriter( s.getOutputStream() ));
    
             bw.write( "GET / HTTP/1.0\n\n" );
             bw.flush();
    
             Output = new FileOutputStream("myfile.txt");
             file = new PrintStream(Output);
             file.println (line);
            }
          catch( IOException e )  // catch any errors
          {
             System.out.println( "There was an IOException error!" );
          }
       }
    }
    I've tried every combination...moving the file/Output lines all over.
    What am I not doing right (besides everything)?

    ~YC

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Your heading in the wrong direction now. The original code was fine for the most part. You need to understand what is going on with each line of code if you want to do this correctly. Just moving lines of code around in hopes that things will start magically working is not the way to learn to program. Been there done that, it doesn't work.

    You should go through the original code and learn what each method is doing and what is happening when you create new objects or why you need to create the objects. Learn what each line of code means and what it does and why we need to use it.

    Here is the original code that you posted with comments to describe what is happening:

    PHP Code:

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

    public class 
    scrapeSite
    {
       public static 
    void mainString[] args )
       {

         
    // you need enclose certain types of code in try catch blocks
        //  that might generate exceptions.  Literally Try to do this but if it
        // something goes wrong Catch the exception and do something else instead
          
    try
          {
             
    // data member declarations
             
    String line// a string variable
             
    BufferedReader br// buffered reader object
             
    BufferedWriter bw// a file output stream object  

            // data member declaration and object instantiation in one line
             // create a connection to 'www.yahoo.com' on port 80
             
    Socket s = new Socket"www.yahoo.com"80 ); // Socket object is declared and instantiated (created)

             // create the reader and writer objects
             
    br = new BufferedReader( new InputStreamReaders.getInputStream() )); // Buffered reader object is created and passed a parameter of a new InputStreamReader object which is passed a parameter of the socket object we created earlier

             
    bw = new BufferedWriter( new OutputStreamWriters.getOutputStream() )); // Buffered writer object is created and passed a parameter of a new OutputStreamWriter object which is passed a parameter of the socket object we created earlier

             // request the 'root' page
             
    bw.write"GET / HTTP/1.0\n\n" ); // we write this string to output stream
             
    bw.flush(); // we now flush what was in the output stream

             // while more lines, output to the standard output stream - Duh
             // so keep looping and executing what is inside this loop until you reach the last line of text that gets read in
             
    while( (line br.readLine()) != null )
             {
                
    System.out.printlnline ); // self explanatory
             
    }
          }
          catch( 
    IOException e )  // catch any errors - Duh
          
    {
             
    System.out.println"There was an IOException error!" );
          }
       }

    Now in your modified code you added in objects to output the text that is being read in to write it to a file instead of printing it to the screen. The problem you ran into though is that your output file only contained the last line of text that was read in. Why is that? Your code was actually reading in every line of text from that website and it was even writing every line to that output file. The problem is you were overwriting the file each time the while loop ran. You were creating new instances of the FileOutputStream and PrintStream every time the loop executed. Do you need to create a new instance of these object with every iteration of the loop? No. You only need to create one instance. Once those objects are created you can use the methods of those objects to process the incoming lines and write them to the output file. Just follow your original example.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You should also learn to understand the scope of data members. Do you really need to make all your data members public and static? Not necessarily. Learn what public means and when to use it and what static means and when to use it. Learn where and when you should declare and instantiate objects. Leanr what the methods of these object are for and how they work. You have to understand the code if you want to learn to program.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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