# How to update a file?

**URL:** <https://www.ruby-forum.com/t/how-to-update-a-file/137201>\
**Category:** Ruby\
**Created:** [May 9, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/how-to-update-a-file/137201 "2008-05-09T23:25:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 9, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/how-to-update-a-file/137201/1 "2008-05-09T23:25:25Z")

</div>

Hi there

Does anyone know how to write a string at the end of a file?

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 11, 2008, 2:29am UTC](https://www.ruby-forum.com/t/how-to-update-a-file/137201/2 "2008-05-11T02:29:03Z")

</div>

Cheyne Li wrote:

> Hi there
> 
> Does anyone know how to write a string at the end of a file?

contents = IO.read(“data.txt”)  
print contents  
puts

File.open(“data.txt”, “a”) do |file|  
file.print(“this is the last line\n”)  
end

new\_contents = IO.read(“data.txt”)  
print new\_contents

–output:–  
hello  
world  
goodbye

hello  
world  
goodbye  
this is the last line
