Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creation of .txt file from contents of a URL

 
Dhee raj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I wanted to write a method that takes in a URL and creates a .txt file
(containing the text part present in the page).It should also create
text files from all the links present in the main URL.Wanted to know
the best way to achieve this.

Thanks in advance.
 
Tim Moores
Bartender
Posts: 2789
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest might be to use new URL("...").getContent() which returns an InputStream, the contents of which you can then save to a file via FileOutputStream.

Extracting links and treating them similarly is tougher. I'd use a library like HtmlUnit for that.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:The easiest might be to use new URL("...").getContent() which returns an InputStream

It actually returns an Object which may or may not be an InputStream. The proper way is to use new URL("...").openStream() which is shorthand for new URL("...").openConnection().getInputStream().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic