Results 1 to 4 of 4
  1. #1
    capjoel is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Take a look into the API doc and at the other contructors -->
    FileWriter
    :)

  3. #3
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    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"

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

Similar Threads

  1. What do these lines mean?
    By coding in forum New To Java
    Replies: 5
    Last Post: 02-02-2011, 04:01 AM
  2. SAX parsing new lines
    By fedekun in forum XML
    Replies: 0
    Last Post: 12-29-2010, 09:30 PM
  3. add a space after 10 lines?
    By ukemasta in forum New To Java
    Replies: 10
    Last Post: 01-20-2010, 01:12 PM
  4. Anyway to fix the lines so they dun shift?
    By PeterFeng in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 11:26 AM
  5. Random Lines
    By Urgle in forum New To Java
    Replies: 29
    Last Post: 11-12-2008, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •