April 29, 2020

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. More additional Information On Ruby On Rails Course

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.

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.

# 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();

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”.

Syntax

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

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

# 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");

To get in-depth knowledge on Ruby On Rails Online Training

# Read the entire content from a file

print(fileobject.read());

# Closing a file

fileobject.close();

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.

# 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")

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.

Take your career to new heights of success with an Ruby, enroll for live free demo on Ruby On Rails Training