Results 1 to 4 of 4
Thread: Cannot Add new Lines
- 04-01-2011, 01:51 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
Cannot Add new Lines
Hi, I'm trying to write a program that can be used to update a text file, I've got it so it will write a line depending on what the user inputs but each time it runs it overwrites what is already in the file rather than writing it in the same document
Java Code:import java.util.Scanner; import java.io.*; public class dateupdate { public static void main (String[] args)throws IOException { Scanner inputVal = new Scanner(System.in); FileWriter first = new FileWriter(new File("first.txt")); //here I am trying to get it to read what is already in the file and writing that to the document again BufferedReader in = new BufferedReader(new FileReader("first.txt")); String str; while ((str = in.readLine()) != null) { first.write(str); System.out.println(str); } in.close(); String service,time; String d; //This is where the user inputs the data System.out.println("Please type in the date of the service"); d=inputVal.nextLine(); System.out.println("Please type in the name of the service"); service=inputVal.nextLine(); System.out.println("Please write in the time using 24hr clock e.g.10.00am"); time=inputVal.nextLine(); //This is where it writes the new data //Write Date first.write(d); //The following lines writes //Write st,nd,rd,th if (d.equals("1")) {first.write("st");} else if (d.equals("2")) {first.write("nd");} else if (d.equals("3")) {first.write("rd");} else {first.write("th");} first.write(" "); first.write(service); first.write(" "); first.write(time); first.close(); //Closes the file } // main method } // Firstfile class
- 04-01-2011, 02:18 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Take a look into the API doc and at the other contructors -->
FileWriter
:)
- 04-01-2011, 03:23 PM #3
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
I suggest reading the whole file first, append all lines to a stringbuffer/stringbuilder, once all lines are read, write the stringbuffer/stringbuilder object back to file and then start writing new lines.
Otherwise, you can keep different name for new file. Once you are done with writing - delete the original file and rename the new file to "first.txt"
- 04-01-2011, 06:33 PM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Similar Threads
-
What do these lines mean?
By coding in forum New To JavaReplies: 5Last Post: 02-02-2011, 03:01 AM -
SAX parsing new lines
By fedekun in forum XMLReplies: 0Last Post: 12-29-2010, 08:30 PM -
add a space after 10 lines?
By ukemasta in forum New To JavaReplies: 10Last Post: 01-20-2010, 12:12 PM -
Anyway to fix the lines so they dun shift?
By PeterFeng in forum New To JavaReplies: 0Last Post: 01-14-2009, 10:26 AM -
Random Lines
By Urgle in forum New To JavaReplies: 29Last Post: 11-12-2008, 03:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks