Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-01-2010
Member
 
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.
Reply With Quote
  #2  
Old 18-01-2010
Member
 
Join Date: May 2008
Posts: 2,293
Re: Append text to end of file

Hello,
The code works very well. The data are written at the end of the file.
Quote:
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.
Reply With Quote
  #3  
Old 18-01-2010
Member
 
Join Date: May 2008
Posts: 2,383
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.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #4  
Old 18-01-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
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
Quote:
this is an example
this is an example
this is an example
this is an example
this is an example
Reply With Quote
  #5  
Old 18-01-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Append text to end of file

Hello,
Quote:
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.
Reply With Quote
  #6  
Old 18-01-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 12:48 PM.