| | |
Writing to a file
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
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
Views: 2052 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api appinventor apple applet application arguments array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component database draw eclipse error event exception file fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaprojects jetbrains jmf jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie notdisplaying number object oracle page print problem program programming project qt recursion scanner screen server set size sms socket sort spamblocker sql string swing system test threads time transfer tree variablebinding windows xor





