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.
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.
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
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.
fileobject = File.new("filename.txt", "r") fileobject.sysread(20) fileobject.close()
Below is the implementation for reading the content from a file.
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.
To check whether the file exists or not
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.
Please Login to comment...