Writing to a file

Thread Solved

Join Date: Nov 2007
Posts: 20
Reputation: im_desperate is an unknown quantity at this point 
Solved Threads: 0
im_desperate im_desperate is offline Offline
Newbie Poster

Writing to a file

 
0
  #1
Dec 9th, 2007
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.
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 71
Reputation: indienick is an unknown quantity at this point 
Solved Threads: 2
indienick's Avatar
indienick indienick is offline Offline
Junior Poster in Training

Re: Writing to a file

 
1
  #2
Dec 10th, 2007
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.

  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.
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg

Don't tell me to "google it" - I already have.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 20
Reputation: im_desperate is an unknown quantity at this point 
Solved Threads: 0
im_desperate im_desperate is offline Offline
Newbie Poster

Re: Writing to a file

 
0
  #3
Dec 12th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC