Changes

Working with Files in Ruby

3,203 bytes added, 16:31, 29 November 2007
no edit summary
File.delete("newfile.txt")
=> 1
</pre>
 
== 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:
 
<pre>
File.exists?("temp.txt")
=> true
</pre>
 
To find out if the file is really a file as opposed to, for example, a directory use the ''file?'' method:
 
<pre>
File.file?("ruby")
=> false
</pre>
 
Similarly, find out if it is a directory with the ''directory?'' method:
 
<pre>
File.directory?("ruby")
=> true
</pre>
 
To identify whether a file is readable, writable or executable, use the ''readable?'', ''writable?'' and ''executable?'' methods:
 
<pre>
File.readable?("temp.txt")
=> true
 
File.writable?("temp.txt")
=> true
 
File.executable?("temp.txt")
=> false
</pre>
 
Get the size of a file with, yes you guessed it, the ''size'' method:
 
<pre>
File.size("temp.txt")
=> 99
</pre>
 
And find if a file is empty (i.e zero length) with the ''zero?'' method:
 
<pre>
File.zero?("temp.txt")
=> false
</pre>
 
Find out the type of the file with the ''ftype'' method:
 
<pre>
File.ftype("temp.txt")
=> "file"
 
File.ftype("../ruby")
=> "directory"
 
File.ftype("/dev/sda5")
=> "blockSpecial"
</pre>
 
Finally, find out the creatation, modify and access times with ''ctime'', ''mtime'' and ''atime'':
 
<pre>
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
</pre>
 
== 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:
 
<pre>
myfile = File.open("temp.txt")
=> #<File:temp.txt>
 
myfile.readline
=> "This is a test file\n"
 
myfile.readline
=> "It contains some example lines\n"
</pre>
 
Alternatively, we can use the ''each'' method to read the entire file:
 
<pre>
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
</pre>
 
It is also possible to extract data from a file on character by character basis using the ''getc'' method:
 
<pre>
myfile = File.open("Hello.txt")
=> #<File:temp.txt>
 
myfile.getc.chr
=> "H"
myfile.getc.chr
=> "e"
myfile.getc.chr
=> "l"
</pre>
 
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:
 
<pre>
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"
</pre>