Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-18-2007, 10:40 PM
Member
 
Join Date: Jul 2007
Posts: 35
silvia is on a distinguished road
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:

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 07:48 AM
Member
 
Join Date: Jul 2007
Posts: 40
barney is on a distinguished road
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?
Greetings.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text Output for a library dream_noir Advanced Java 0 04-14-2008 07:31 AM
How to read a text file from a Java Archive File Java Tip Java Tips 0 02-08-2008 11:13 AM
File fp = new File(filePath);fp.exists() does not yeild proper result ganeshp Advanced Java 1 12-27-2007 01:42 AM
how to write the output of the console to a file fred New To Java 1 07-24-2007 04:02 AM
count character in text file as input file aNNuur New To Java 0 06-18-2007 08:46 AM


All times are GMT +3. The time now is 11:36 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org