Reading the Contents of a File : read « File Directory « Ruby






Reading the Contents of a File


# Put some stuff into a file.
open('sample_file', 'w') do |f|
  f.write("This is line one.\nThis is line two.")
end

# Then read it back out.
open('sample_file') { |f| puts f.read }

 








Related examples in the same category

1.read an arbitrary number of bytes from a file into a single variable using read:
2.Read 50 characters
3.Read file by chunk size