How to Read and Writes Files in Ruby

RubyServer Side ProgrammingProgramming

Ruby provides us with different methods that we can use to handle files. In simple terms, file handling involves different processes such as creating a new file, reading the contents present in a file, writing some content to a file, appending content to a file, deleting a file and more.

There are different modes that one can use to do file handling in Ruby. These are −

  • r = Read-only mode

  • r+ = Read-Write mode

  • = Write-mode only

  • w+ = Read-write mode

And some more. These four mentioned modes are the most commonly used ones, when it comes to file handling in Ruby. In this article, we will explore all the major cases of file handling one by one with the help of examples.

Creating a File in Ruby

Let's start with the most common and useful one, how to create a file in Ruby. Consider the code shown below.

# Creating a file
fileObj = File.new("tutorials.txt", "w+");

# Writing to the file
fileObj.syswrite("This file contains knowledge!");

# Closing a file
fileObj.close();

When we want to create a new file in Ruby, we use the new() method of the File module, and it takes two arguments. The first argument is the name of the file and the second argument is the mode in which we want to open the file.

If we write the following code on any Ruby IDE, then a new file will be created in the same directory. We can confirm the same by opening the file using the cat command.

Command

cat tutorials.txt

It will produce the following output.

This file contains knowledge!

Opening a File in Ruby

Now we will see how to open a file in Ruby. There are multiple methods that we can use for opening a file in Ruby. There are two methods which are widely used − the sysread(n) and the read() method.

The open method is used to open the file, while the sysread(n) is used to read the first "n" characters from a file, and the read() method is used to read the entire file content.

Example

Consider the code shown below.

fileObject = File.open("tutorials.txt","r");
puts(fileObject.sysread(10));
fileObject.close();

In the above code, we are reading the first 10 characters of the file named "tutorials.txt".

It will produce the following output.

This file

Here is the another example of opening a file in Ruby.

fileObject = File.open("tutorials.txt","r");
print(fileObject.read());
fileObject.close();

It will produce the following output.

This file contains knowledge!

Now let's try to rename a file in Ruby.

Renaming a File in Ruby

To rename a file in Ruby, we need to use the rename method that is present in the File module.

puts File.rename("tutorials.txt", "sample.txt")

This command will change the name of the file from "tutorials.txt" to "sample.txt". And, it will display the following output on the console −

0

Deleting a File in Ruby

Last but not the least, let's consider how we can delete a file in Ruby. To delete a file, we just need to call the delete method present in the File module.

puts File.delete("sample.txt")

If we write the following code on any Ruby IDE, then we will get the following output on the console −

1
raja
Updated on 12-Apr-2022 08:22:01

Advertisements