| | |
Writing to a file
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 20
Reputation:
Solved Threads: 0
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.
and also another thing i dont know how to finish writing to the text file.
Thanks in advance
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)
import java.io.*; public class Writing{ public static void main(String [] args) { try{ File file = new File("mytext.txt"); PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter(file, true))); } catch (IOException e) { System.err.println ("Unable to write to file"); System.exit(-1); } } }
and also another thing i dont know how to finish writing to the text file.
Thanks in advance
I haven't written any I/O Java code in a while, but I do remember using
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.
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)
import java.io.*; import java.util.Scanner; ... try { PrintStream ps = new PrintStream(new File("file.txt")); Scanner s = new Scanner(System.in); String lineBuffer; lineBuffer = s.nextLine(); ps.println(lineBuffer); } catch (IOException ioex) { ... } finally { ps.close(); s.close(); } return; ...
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.
-Ginsburg
Don't tell me to "google it" - I already have.
![]() |
Similar Threads
- Redirecting console output to file (Java)
- Read/write to same file > once, Help (C++)
- ofstream - detect if file has been deleted between open and close (C++)
- A question about file streaming (C++)
- writing to a file (Java)
- Saving a file (Java)
- file input and output (C)
Other Threads in the Java Forum
- Previous Thread: Java - enter string
- Next Thread: Mutiple Object references
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working





