File Handling in Ruby

It is a way of processing a file such as creating a new file, reading content in a file, writing content to a file, appending content to a file, renaming the file and deleting the file.

Common modes for File Handling
“r” : Read-only mode for a file.
“r+” : Read-Write mode for a file.
“w” : Write-only mode for a file.
“w+” : Read-Write mode for a file.
“a” : Write-only mode, if file exists it will append the data otherwise a new file will be created.
“a+” : Read and Write mode, if file exists it will append the data otherwise a new file will be created.

Creating a new file
We can create a new File using File.new() method for reading, writing or for both, according to the mode string. we can use fileobject.close() method to close that file.

Writing to the file
With the help of syswrite method, you can write content into a file. File needs to be opened in write mode for this method.The new content will over write the old content in an already existing file.

Syntax



fileobject = File.new("filename.txt", "mode")
fileobject.syswrite("Text to write into the file")
fileobject.close()

Below is the implementation for creating a new file and writing into it.

filter_none

edit
close

play_arrow

link
brightness_4
code

# File Handling Program
  
# Creating a file
fileobject = File.new("sample.txt", "w+"); 
  
# Writing to the file
fileobject.syswrite("File Handling");    
  
 # Closing a file
fileobject.close();                        

chevron_right


Output:

Description:
A text file named sample.txt is created with read and write permission. The content “File Handling” is written to the file using syswrite method. Finally close the file. When you open the sample.txt file, it will contain the string “File Handling”.

Reading a file
There are three different methods to read a file.

1. fileobject.sysread(20) – Return only the first 20 characters from that file
2. fileobject.read – Return the entire content from a file
3. fileobject.readlines – Return the values as an array of lines

sysread Method
The sysread method is also used to read the content of a file. With the help of this method you can open a file in any mode. It will print till n characters mentioned in sysread method from the file.

Syntax

fileobject = File.new("filename.txt", "r")
fileobject.sysread(20)
fileobject.close()

Below is the implementation for reading the content from a file.

filter_none

edit
close

play_arrow

link
brightness_4
code

# File Handling Program
  
# Opening a file
fileobject = File.open("sample.txt", "r"); 
  
# Reading the first n characters from a file
puts(fileobject.sysread(21));
  
# Closing a file
fileobject.close();                         
  
# Opening a file
fileobject = File.open("sample.txt", "r");     
  
# Read the values as an array of lines
print(fileobject.readlines);             
puts
  
# Closing a file
fileobject.close();                        
  
# Opening a file
fileobject = File.open("sample.txt", "r"); 
  
# Read the entire content from a file
print(fileobject.read());    
  
# Closing a file
fileobject.close();                         

chevron_right


Output:

Description:
The sample text file contains the string “File handling in Ruby language”. The file is opened with read-only permission. The above output shows the different way of reading the file and print it. The sysread method read only 21 characters and then print the first 21 characters. The readlines method read the values as an array of lines. The read method read the entire content as a string from the file.

Renaming and Deleting a file
Ruby files are renamed using rename method and deleted using delete method.

File Inquiries
To check whether the file exists or not

filter_none

edit
close

play_arrow

link
brightness_4
code

# Rename the file name
puts File.rename("sample.txt", "newSample.txt"
  
# Delete the existing file
puts File.delete("sample1.txt")    
  
# Checking the old filename is existing or not 
puts File.file?("sample.txt")
  
# Checking the renamed file is exiting or not
puts File.file?("newSample.txt")
  
# Checking the file have read permission
puts File.readable?("newSample.txt")
  
 # Checking the file have write permission
puts File.writable?("newSample.txt")        

chevron_right


Output:

Description:
The rename method is used to rename the file contain the old name with the new name and the rename method print ‘0’ for the file is renamed, otherwise print ‘1’.The delete method is used to delete the exiting file and it will print ‘1’ if the file is deleted otherwise print ‘0. The file? method is used to check if the file is exists or not. It will return false if the file is not exists and otherwise true. In our case the sample.txt text file is not exists because we renamed it to newSample.txt file so it will return false. The newSample file is existing, so it will return true. The newSample file is having both read and write permission, so it will return true for last two statement.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.