Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a text to file succinctly

Tags:

Instead of writing

File.open("foo.txt", "w"){|f| f.write("foo")} 

We can write it

File.write("foo.txt", "foo") 

Is there simpler way to write this one?

File.open("foo.txt", "a"){|f| f.write("foo")} 
like image 409
ironsand Avatar asked Jan 15 '15 04:01

ironsand


People also ask

How do I append a text file to another text file?

You do this by using the append redirection symbol, ``>>''. To append one file to the end of another, type cat, the file you want to append, then >>, then the file you want to append to, and press <Enter>.

How do you write and append to a file in Java?

Instantiate the BufferedWriter class. By passing the FileWriter object as an argument to its constructor. Write data to the file using the write() method.

How do I add text to an existing file in Java?

Likewise, the text to be added is stored in the variable text . Then, inside a try-catch block we use Files ' write() method to append text to the existing file. The write() method takes the path of the given file, the text to the written, and how the file should be open for writing.


2 Answers

This has been answered in great depth already:

can you create / write / append a string to a file in a single line in Ruby

File.write('some-file.txt', 'here is some text', File.size('some-file.txt'), mode: 'a') 
like image 150
karns Avatar answered Oct 21 '22 20:10

karns


f = File.open('foo.txt', 'a') f.write('foo') f.close 
like image 20
Rustam A. Gasanov Avatar answered Oct 21 '22 21:10

Rustam A. Gasanov