A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • one year ago

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

  • This Question is Open
  1. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    oops I mean "programming by doing"

  2. woodrow73
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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(); } } } } ```

  3. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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`.

  4. Thebadyboy
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @woodrow73 u just blew my mind and yes follow his codeing

  5. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.)

  6. woodrow73
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.