Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - READING FILES


So, in the previous tutorial, we have created a file named myfirstfile.txt, with the below contents,

java_Collections

Now, let us see, how can we read the above file in Ruby.


How to read the entire file?


We can use the read() Method to read the total file.


There are two modes for reading a file :

  1. "r" - Read Mode - This is the default mode. Used to opens a file for reading and throws an error if the file is not present.

  2. "r+" - Read and Write Mode - Read and Write mode is used to write something from the beginning of the file. And if the file is not present, throws an error.

Example :



myfile = open("myfirstfile.txt", "r")
str = myfile.read()
puts str
myfile.close()


Output :



  In a huge pond,
  there lived many fish.
  They were arrogant and
  never listened to anyone.
  In this pond,
  there also lived
  a kind-hearted crocodile.
  1. So, what we have done in the above example is, opened the file in read Mode(i.e. r).

    myfile = open("myfirstfile.txt", "r")

  2. Then we have used the read() Method to read the entire contents of the file.

    str = myfile.read()
    
    And initialised the contents of the file in a variable, str as a String.

    java_Collections

  3. After that we have used the print statement to print the contents of str(The string that contains the contents of the file).

    puts str

  4. Finally, we close the file using the close() Method.

    myfile.close()

How to read only a single line in the File?


We can use the readline() Method to read the first line of the file.


Example :



myfile = open("myfirstfile.txt", "r")
str = myfile.readline()
puts str
myfile.close()


Output :



  In a huge pond,

So, in the above example, we have used the readline() Method to read the first line of the file.


str = myfile.readline()

Rest of the lines are just the same.


How to read the file by specifying the number of characters?


We can use the number of character with the read() Method to read the File by number of characters.


Example :



myfile = open("myfirstfile.txt", "r")
str = myfile.read(9)
puts str
myfile.close()


Output :



  In a huge

So, in the above example, we have used the read() Method and specified 9 as argument to read the first 9 characters of the file.


str = myfile.read(9)

And got the below output.


In a huge

How to get the current position of the file pointer?


In the above example, we have used the read() Method and specified 9 as argument to read the first 9 characters of the file.


Now, the file pointer should be at 9.


And we can get the current file pointer using the tell() Method.


Example :



myfile = open("myfirstfile.txt", "r")
str = myfile.read(9)
puts myfile.tell()
myfile.close()


Output :



  9

And all we have done is used the tell() Method to get the position of the file pointer.


puts myfile.tell()

And as we know the current file pointer is at 9. So we got 9 as output.


How to change the current position of the file pointer?


In the above example, we have used the tell() Method to identify the current file pointer position.


Now, what if we want to change the file pointer, so that it points to some other location.


And we can achieve it using the seek() Method.


Let us see in the below example.


Example :



myfile = open("myfirstfile.txt", "r")
str = myfile.read(9)
puts myfile.tell()
myfile.seek(0)
puts myfile.tell()
myfile.close()


Output :



  9
  0

So, if we see the output, we can see that the tell() Method is pointing to position 9.


puts myfile.tell()

Then we have used the seek() Method to reset the position of the pointer to 0.


myfile.seek(0)

And if you see the next tell() Method,


puts myfile.tell()

It points to location 0. As