• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with files  RSS feed

 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a Java program that outputs your name, today's date, and five lines of your favorite PG-rated poem to the file "poem.txt".


I need help on figuring out how to write this program.  I don't know how to write to a file from Java.   Thanks!!



this is what I have so far...
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the PrintWriter class.  It has many constructors for creating a file and methods for writing to a file.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norman Radder wrote:Look at the PrintWriter class.  It has many constructors for creating a file and methods for writing to a file.


Could you show me an example please?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this thread. Note that in Java 8, you only need two lines of code.
 
George Marts
Greenhorn
Posts: 3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Sousie wrote:
Norman Radder wrote:Look at the PrintWriter class.  It has many constructors for creating a file and methods for writing to a file.


Could you show me an example please?

An easy example of PrintWriter would be on the lines of
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are posting code please make sure it compiles before posting:
It should be:



Although really it should be:


Using a try block (either try-with-resources or the older try-finally) is the only way to ensure your resource is closed regardless of what exceptions your code throws.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!