Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Writing to a text file with BufferedWriter ?

 
Ranch Hand
Posts: 257
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: 74069
332
  • 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: 257
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: 74069
332
  • 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.
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic