943,816 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 4141
  • Java RSS
Dec 9th, 2007
0

Writing to a file

Expand Post »
Hey
i have to write a program that will prompt the user to write sentences and those sentences will be written until the user type "stop".
i though of doing such a thing but its not doing what i want because i want each input to be on a new line.
Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2.  
  3. public class Writing{
  4. public static void main(String [] args)
  5. {
  6. try{
  7. File file = new File("mytext.txt");
  8. PrintWriter out =
  9. new PrintWriter(
  10. new BufferedWriter(
  11. new FileWriter(file, true)));
  12. }
  13. catch (IOException e)
  14. {
  15. System.err.println ("Unable to write to file");
  16. System.exit(-1);
  17. }
  18.  
  19. }
  20. }


and also another thing i dont know how to finish writing to the text file.



Thanks in advance
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
im_desperate is offline Offline
20 posts
since Nov 2007
Dec 10th, 2007
1

Re: Writing to a file

I haven't written any I/O Java code in a while, but I do remember using java.io.PrintStream which is a buffered stream, so you don't need to create a whole mess of inline class instances.

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.util.Scanner;
  3. ...
  4. try {
  5. PrintStream ps = new PrintStream(new File("file.txt"));
  6. Scanner s = new Scanner(System.in);
  7. String lineBuffer;
  8.  
  9. lineBuffer = s.nextLine();
  10.  
  11. ps.println(lineBuffer);
  12. } catch (IOException ioex) {
  13. ...
  14. } finally {
  15. ps.close();
  16. s.close();
  17. }
  18. return;
  19. ...
You may want to refine this code a little, and cover any other exceptions that may be thrown; I just wrote this out quickly and don't have time to check it, but it should work.

I haven't added any of your particulars into this, and for this I apologize, but I'm sure you can do it quite easily.
Last edited by indienick; Dec 10th, 2007 at 12:37 am.
Reputation Points: 23
Solved Threads: 2
Junior Poster in Training
indienick is offline Offline
71 posts
since Aug 2005
Dec 12th, 2007
0

Re: Writing to a file

thanks for the code
but actually the main thing that i wanted was someone to help me find a way when the user types a specific word to stop the program.
thanks in advance
Reputation Points: 10
Solved Threads: 1
Newbie Poster
im_desperate is offline Offline
20 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java - enter string
Next Thread in Java Forum Timeline: Mutiple Object references





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC