Results 1 to 2 of 2
- 07-18-2007, 08:40 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 35
- Rep Power
- 0
Simply output the result to a text 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:
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 thisJava 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!" ); } } }
It almost works, except only the last line gets writtent to the txt file.Java 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!" ); } } }
Am I on the right track?
Thanks
- 08-07-2007, 05:48 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
File fp = new File(filePath);fp.exists() does not yeild proper result
By ganeshp in forum Advanced JavaReplies: 2Last Post: 04-07-2009, 06:25 AM -
Text Output for a library
By dream_noir in forum Advanced JavaReplies: 0Last Post: 04-14-2008, 05:31 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM -
how to write the output of the console to a file
By fred in forum New To JavaReplies: 1Last Post: 07-24-2007, 02:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks