• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Write the output to a file

 
Greenhorn
Posts: 25
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im trying to write the contents of webpage to a file



its creating a blank file...the contents are not being written...please help me with this
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you open a new file every time the handleText() method is called?
 
Prasad Rooge
Greenhorn
Posts: 25
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for reply

i have created the file outside the outside the handleText ()


 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that just creates a Java "File" object. This does not create anything in your operating system's file system. I should have asked why you repeatedly re-opened the file.
 
Prasad Rooge
Greenhorn
Posts: 25
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The char array data is only accessible inside the handleText() and it changes everytime the method is called. So im trying to append the into the file.
Sorry for any mistake i'm a beginner in java.

Thanks
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Rooge wrote:The char array data is only accessible inside the handleText() and it changes everytime the method is called.


Then doesn't it make sense to make the File (or perhaps even better, its FileOutputStream) accessible in the method too? What you're running into is an example of "tight coupling", and one of the ways to solve it would be to pass the FileOutputStream to the method.

So im trying to append the [String] into the file.


I wouldn't advise that. Opening and closing files is expensive. Why not just append what you need to the html you just read in, and write the whole file in one go?

Winston
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note, every time you do the contents of the file are purged and the file becomes empty.

It goes back to Winston's suggestion - don't open FileOutputStream multiple times. Every time you open it, the earlier contents will vanish.
 
Prasad Rooge
Greenhorn
Posts: 25
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for your suggestions
Well i got the desired output... i made the following changes

append the data to Stringbuffer and write to a file in one go.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Rooge wrote:Well i got the desired output... i made the following changes...append the data to Stringbuffer and write to a file in one go.


Hmmm. Not the best I suspect, because all that appears to do is duplicate the input; but if it works...

A few tips for you:
  • 1. Avoid static variables. They are almost invariably evil.
  • 2. Write methods that do one thing.
  • 3. Write methods that return something useful. That is to say: apart from things like setters (and main() of course), avoid writing methods that have void in the signature. Methods that return something can be combined with others, or used in assignments, which increases cohesion; usually a good thing.

  • For example, your handleText() method could look something like:which would then hand you back a String containing all the stuff in your healthcare file, plus whatever you passed in 'data'; and furthermore, it's completely self-contained. BTW, I removed the 'pos' parameter, because you don't appear to use it.

    It's just one possibility though; and there may well be far better ones.

    HIH

    Winston
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic