• 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

Writing to a text file with BufferedWriter ?

 
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am reading from one text files and writing the output to a new text file.

My code went and created the output text file, but does not populate it, i.e. it is an empty file.

It does print the inbound text files to the screen nicely, with line number.

What have I done wrong this time?


 
Marshal
Posts: 79231
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know. That is quite old‑fashioned code; you should use the new Date classes rather than java.util.Date.
One thing: you should close the “outside” objects. Your outermost reader is the line number reader, so you close that, and you don't need to close the other two readers. Your outermost writer is the buffered writer but you haven't closed that. If you don't close the buffered writer you may never flush it and you get nothing in the output file. Not sure whether that is the cause of your problem, but it is worth considering.

Suggest: Close only the lnr and the bw. Find out how to open the writers with try‑with‑resources and you can forget about closing because it is all done automatically.
 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't know. That is quite old‑fashioned code; you should use the new Date classes rather than java.util.Date.
One thing: you should close the “outside” objects. Your outermost reader is the line number reader, so you close that, and you don't need to close the other two readers. Your outermost writer is the buffered writer but you haven't closed that. If you don't close the buffered writer you may never flush it and you get nothing in the output file. Not sure whether that is the cause of your problem, but it is worth considering.

Suggest: Close only the lnr and the bw. Find out how to open the writers with try‑with‑resources and you can forget about closing because it is all done automatically.



Thanks, it works. Amazing how the order of closing the readers make such a big difference.

About old fashioned codes, the last time I Java-ed was 10 years ago, it has been a learning experience coming back up to speed. Sorry about that.
 
Campbell Ritchie
Marshal
Posts: 79231
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done

Learn the new date classes from the Java™ Tutorials and see how much better they are than the old classes.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic