File.read and File.write
In this page:
File.read and File.write
File.read("path") reads an entire file's contents into a single string in one call, the simplest way to load a small-to-medium text file. File.write("path", content) writes a string to a file, creating or overwriting it. Both are convenient class-level methods for simple, one-shot file operations.
Note: File.write overwrites the entire file by default -- there's no separate append mode without extra options.
Example: File.read and File.write
File.write("greeting.txt", "Hello from File.write!")
content = File.read("greeting.txt")
puts content
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: