anonymous
  • anonymous
Beginner Java question. I am doing 'the hard way' and started on the file input etc. The question asks to make a letter (done) and instead of displaying, put it in a file... with no explanation as to how to do that except close(); Well here is the link to the project: http://programmingbydoing.com/a/letter-revisited.html
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
chestercat
  • chestercat
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
oops I mean "programming by doing"
woodrow73
  • woodrow73
If it's supposed to be teaching you, but has no explanation on how to do it.. I'd recommend learning elsewhere.. honestly Here's a quick snippet of code that prints my name to a file; ``` import java.io.*; public class Dog { public static void main(String[] args) { new Dog(); } public Dog() { PrintWriter pw = null; PrintWriter pw2 = null; //the below path will be in same //directory as this program File writeTo = new File("myname.txt"); //the below path will be placed //exactly where you specify File otherPath = new File( "C:\\programs\\wood\\myname.txt"); try{ pw = new PrintWriter(writeTo); pw.println("Wood"); //name has been printed to myname.txt pw2 = new PrintWriter(otherPath); //good practice to often use flush(); when printing a lot, //otherwise it may not work. pw2.print("My "); pw2.flush(); pw2.print("name "); pw2.flush(); pw2.print("is "); pw2.flush(); pw2.println("wood."); pw2.flush(); pw2.println("Nice to meet you."); }catch(IOException e) { e.printStackTrace(); }finally { if(pw != null) { pw.close(); } if(pw2 != null) { pw2.close(); } } } } ```
anonymous
  • anonymous
You should only use `flush` if there is a fear that the program will crash without closing the file properly. I think there is no such fear when you are just doing multiple print statements. The `PrintWriter` constructor is the only thing that would throw an `IOException`.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

Thebadyboy
  • Thebadyboy
@woodrow73 u just blew my mind and yes follow his codeing
anonymous
  • anonymous
Cool! It works! Thankyou! (The other parts of the course at least have an example code to examine, but for some reason in this topic which is completely unfamiliar to me, it didn't.)
woodrow73
  • woodrow73
Sure; you're welcome. Also, the FileWriter class can be used to append to a file; and the Scanner class to read in from a file. Thanks for the tips @wio - seems that's a bad habit I developed when learning java for some reason

Looking for something else?

Not the answer you are looking for? Search for more explanations.