Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Dealing with Files in Ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 388
    Comment on it

    In my previous blog Dealing with Directories in Ruby, I explained you, how we can play with directories in ruby. Today I am going to tell you how we can interact with files by opening them in different modes and performing read/write operations in files. So files in ruby are basically handled using two ruby classes that are File class and IO class. The most commonly methods used are:

    a) Creating File objects:

    File objects can be created using using 2 methods:

    1) File.open:

    It is used to create a new file object in a mode so that other operations on the file can be performed. You can associate with a block.
     

    File.open("filepath", "mode") do |file|
      #File operations
    end

    The different modes available for a file are:
     

    r    Read-only mode
    r+    Read-write mode
    w    Write-only mode
    w+    Read-write mode
    a    Write-only mode but appends at the last
    a+    Read and write mode and points at the end of the file


    2) File.new:

    It is also used to create a file object but unlike File.open it can't be associated to a block.

    file = File.new("filepath", "mode")
      #File operations
    file.close

    b) Reading contents from file using sysread method:

    This method is used to read the file content. It reads the file on character basis so on the file object if you call this method by passing a number, it will return the characterd upto that position. As it is just for reading the file so you can open file in any mode. Lets see an example suppose we have a file called "nothing.txt" with texts:

    I am a rails developer.
    I am a python developer.
    I am a ruby developer.

    Then if we run sysread like this

    f = File.new("nothing.txt", "r")
    p f.sysread(4)
    
    #=> Output will be
    "I am a"
    

    c) Writing contents to file using syswrite method:


    For writing contents to a file you can use this method. For using this the file must be opened in write/appending mode

    f = File.new("nothing.txt", "w")
    f.syswrite("I am a HTML developer")
    
    Now the file nothing.txt will have content:
    I am a HTML developer

    d) Reading character by character using each_byte:

    This method iterates over each character of the file.

    f = File.new("nothing.txt", "r+")
    f.each_byte {|char| #Perform operation }


    e) Reading Line by Line a file:

    For reading a file by line IO.readlines method is used that stores all the lines in an array: If our file has content

    
    I am a rails developer.
    I am a python developer.
    I am a ruby developer.


    Then this will work like this:

    
    lines = IO.readlines("nothing.txt")
    puts lines[0]
    #=> I am a rails developer.
    puts lines[1]
    #=> I am a python developer.
    puts lines[2]
    #=> I am a ruby developer.


    f) Writing Contents to File using write method:

    Contents can be written to file using this:

    File.open("FILE_PATH", 'w') { |file| file.write("your content") }


    g) Other File Operations:

    To rename a file rename method can be used like this:

    # Rename a file from a1.txt to a2.txt
    File.rename( "a1.txt", "a2.txt" )
    

    To delete a file delete method can be used

    # Delete a file a1.txt
    File.delete("a1.txt")

    Changing the mode/ownership

    # Changing mode of file a.txt to mask value
    
    file = File.new("a.txt", "w")
    file.chmod( 0755 )

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: