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