Working with Files in Ruby
Previous | Table of Contents | Next |
Ruby Directory Handling | Working with Dates and Times in Ruby |
Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99 |
In the previous chapter we looked at how to work with directories. This chapter will look in detail at how to create, open and read and write to files in Ruby. We will then learn how to delete and rename files.
Creating a New File with Ruby
<google>ADSDAQBOX_FLOW</google> New files are created in Ruby using the new method of the File class. The new method accepts two arguments, the first being the name of the file to be created and the second being the mode in which the file is to opened. Supported file modes are shown the following table:
Mode | Description |
---|---|
r | Read only access. Pointer is positioned at start of file. |
r+ | Read and write access. Pointer is positioned at start of file. |
w | Write only access. Pointer is positioned at start of file. |
w+ | Read and write access. Pointer is positioned at start of file. |
a | Write only access. Pointer is positioned at end of file. |
a+ | Read and write access. Pointer is positioned at end of file. |
b | Binary File Mode. Used in conjunction with the above modes. Windows/DOS only. |
With this information in mind we can, therefore, create a new file in "write" mode as follows:
File.new("temp.txt", "w") => #<File:temp.txt>
Opening Existing Files
Existing files may be opened using the open method of the File class:
file = File.open("temp.txt") => #<File:temp.txt>
Note that existing files may be opened in different modes as outlined in the table above. For example, we can open a file in read-only mode:
file = File.open("temp.txt", "r") => #<File:temp.txt>
It is also possible to identify whether a file is already open using the closed? method:
file.closed? => false
Finally, we can close a file using the close method of the Ruby File class:
file = File.open("temp.txt", "r") => #<File:temp.txt> file.close => nil
Renaming and Deleting Files in Ruby
Files are renamed and deleted in Ruby using the rename and delete methods respectively. For example, we can create a new file, rename it and then delete it:
File.new("tempfile.txt", "w") => #<File:tempfile.txt> File.rename("tempfile.txt", "newfile.txt") => 0 File.delete("newfile.txt") => 1
Getting Information about Files
File handling often requires more than just opening files. Sometimes it is necessary to find out information about a file before it is opened. Fortunately the File class provides a range of methods for this very purpose.
To find out if a file already exists, use the exists? method:
File.exists?("temp.txt") => true
To find out if the file is really a file as opposed to, for example, a directory use the file? method:
File.file?("ruby") => false
Similarly, find out if it is a directory with the directory? method:
File.directory?("ruby") => true
To identify whether a file is readable, writable or executable, use the readable?, writable? and executable? methods:
File.readable?("temp.txt") => true File.writable?("temp.txt") => true File.executable?("temp.txt") => false
Get the size of a file with, yes you guessed it, the size method:
File.size("temp.txt") => 99
And find if a file is empty (i.e zero length) with the zero? method:
File.zero?("temp.txt") => false
Find out the type of the file with the ftype method:
File.ftype("temp.txt") => "file" File.ftype("../ruby") => "directory" File.ftype("/dev/sda5") => "blockSpecial"
Finally, find out the create, modify and access times with ctime, mtime and atime:
File.ctime("temp.txt") => Thu Nov 29 10:51:18 EST 2007 File.mtime("temp.txt") => Thu Nov 29 11:14:18 EST 2007 File.atime("temp.txt") => Thu Nov 29 11:14:19 EST 2007
Reading and Writing Files
Once we've opened an existing file or created a new file we need to be able to read from and write to that file. We can read lines from a file using either the readline or each methods:
myfile = File.open("temp.txt") => #<File:temp.txt> myfile.readline => "This is a test file\n" myfile.readline => "It contains some example lines\n"
Alternatively, we can use the each method to read the entire file:
myfile = File.open("temp.txt") => #<File:temp.txt> myfile.each {|line| print line } This is a test file It contains some example lines But other than that It serves no real purpose
It is also possible to extract data from a file on a character by character basis using the getc method:
myfile = File.open("Hello.txt") => #<File:temp.txt> myfile.getc.chr => "H" myfile.getc.chr => "e" myfile.getc.chr => "l"
Needless to say, we can also write to a file using the putc method to write a character at a time and puts to write a string at a time - note the importance of the rewind method call. This moves the file pointer back to the start of the file so we can read what have written:
myfile = File.new("write.txt", "w+") # open file for read and write => #<File:write.txt> myfile.puts("This test line 1") # write a line => nil myfile.puts("This test line 2") # write a second line => nil myfile.rewind # move pointer back to start of file => 0 myfile.readline => "This test line 1\n" myfile.readline => "This test line 2\n"
Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99 |
Previous | Table of Contents | Next |
Ruby Directory Handling | Working with Dates and Times in Ruby |