Results 1 to 6 of 6

Thread: Append text to end of file

  1. #1
    Join Date
    Dec 2009
    Posts
    178

    Append text to end of file

    Hi,
    I want to write some text at the end of a file. For the first time it worked but now it writes everything on the same line. Here is my code, any help on this.
    Code:
    Public void Write(String file, String txt) throws IOException {
    
    FileWriter wr = null;
    		try {
    			Writer = new FileWriter(file, true);
    wr.write(txt, 0, Txt.length());
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} finally {
    			if (wr! = null) {
    				wr.close();
    			}
    		}
    	}
    There are no errors in the code but the text is not written in the exact location of the file. An idea? thank you in advance for your help.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Append text to end of file

    Hello,
    The code works very well. The data are written at the end of the file.
    There are no errors in the code but the text is not written in the exact location of the file.
    In this case you have not forgotten to add a "\ n" at the end of the string that you write to the file? I think you should you the file object here, it would give you more control on the file. Truly speaking I have not understood what is your exact problem. Please explain your problem in more details.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Append text to end of file

    Hello,
    The FAQ makes it clear at the end of the text file. The newline characters are like the others, you must add them to the new lines.
    You can simply try this code.
    Code:
    String text = "text to insert at end of file\ n"; / / Add text and a new line
    String text2 = "\ ntext to be inserted at the end of file "; / / Adds a new line then the text.
    This should definitely work for you, if not then post back your query, but before that please test with the above code.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Append text to end of file

    Hi,
    I have tested it, Indeed with a \ n at the end of the chain there is no return to the line but if you write \ r \ n at the end of the chain you've got a newline
    Code:
    try
    {
    	Write("myfile.txt"," this is an example\ r\ n");
    }
    catch(IOException ex)
    {
     
    }
    If I call 5 times the function Write (), "myfile.txt" contains
    this is an example
    this is an example
    this is an example
    this is an example
    this is an example

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Append text to end of file

    Hello,
    Indeed with a \ n at the end of the chain there is no return to the line but if you write \ r \ n at the end of the chain you've got a newline
    This is not portable, use:
    Code:
    String LineSeparator = System.getProperty("line.separator");
    Write("myfile.txt","this is an example" + LineSeparator);
    I think this would be much more appropriate to use.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Append text to end of file

    Hi,
    In reality, we must use the convention to end of line is needed.
    - If there is files that use \ n, you have to \ n.
    - If there is files that use \ r \ n, we must put \ r \ n.
    - If there is files that are open with the basic tools of the OS, use System.getProperty ( "line.separator").
    - If there is files that use \ r (rare), there must be \ r.
    Hope this information helps you.

Similar Threads

  1. How to rename .Bat-append fixed text in c#
    By Omanand in forum Software Development
    Replies: 5
    Last Post: 05-07-2011, 07:24 PM
  2. How to convert image text into a text file
    By Wadee in forum Windows Software
    Replies: 5
    Last Post: 02-12-2010, 06:33 PM
  3. How to append data to a text file in java?
    By KALANI84 in forum Software Development
    Replies: 4
    Last Post: 05-02-2010, 04:44 PM
  4. How to append an existing file in Java?
    By NinjaZxR in forum Software Development
    Replies: 3
    Last Post: 28-07-2009, 05:55 PM
  5. Reading text string from text file (PHP)
    By Moderate in forum Software Development
    Replies: 3
    Last Post: 16-01-2009, 03:27 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,562,536.84615 seconds with 17 queries