class File

A File object is a representation of a file in the underlying platform.

Class File extends module FileTest, supporting such singleton methods as File.exist?.

About the Examples ↑

Many examples here use these variables:

# English text with newlines.
text = <<~EOT
  First line
  Second line

  Fourth line
  Fifth line
EOT

# Russian text.
russian = "\u{442 435 441 442}" # => "тест"

# Binary data.
data = "\u9990\u9991\u9992\u9993\u9994"

# Text file.
File.write('t.txt', text)

# File with Russian text.
File.write('t.rus', russian)

# File with binary data.
f = File.new('t.dat', 'wb:UTF-16')
f.write(data)
f.close

Access Modes ↑

Methods File.new and File.open each create a File object for a given file path.

String Access Modes ↑

Methods File.new and File.open each may take string argument mode, which:

Read/Write Mode ↑

The read/write mode determines:

  • Whether the file is to be initially truncated.

  • Whether reading is allowed, and if so:

    • The initial read position in the file.

    • Where in the file reading can occur.

  • Whether writing is allowed, and if so:

    • The initial write position in the file.

    • Where in the file writing can occur.

These tables summarize:

Read/Write Modes for Existing File

|------|-----------|----------|----------|----------|-----------|
| R/W  | Initial   |          | Initial  |          | Initial   |
| Mode | Truncate? |  Read    | Read Pos |  Write   | Write Pos |
|------|-----------|----------|----------|----------|-----------|
| 'r'  |    No     | Anywhere |    0     |   Error  |     -     |
| 'w'  |    Yes    |   Error  |    -     | Anywhere |     0     |
| 'a'  |    No     |   Error  |    -     | End only |    End    |
| 'r+' |    No     | Anywhere |    0     | Anywhere |     0     |
| 'w+' |    Yes    | Anywhere |    0     | Anywhere |     0     |
| 'a+' |    No     | Anywhere |   End    | End only |    End    |
|------|-----------|----------|----------|----------|-----------|

Read/Write Modes for \File To Be Created

|------|----------|----------|----------|-----------|
| R/W  |          | Initial  |          | Initial   |
| Mode |  Read    | Read Pos |  Write   | Write Pos |
|------|----------|----------|----------|-----------|
| 'w'  |   Error  |    -     | Anywhere |     0     |
| 'a'  |   Error  |    -     | End only |     0     |
| 'w+' | Anywhere |    0     | Anywhere |     0     |
| 'a+' | Anywhere |    0     | End only |    End    |
|------|----------|----------|----------|-----------|

Note that modes 'r' and 'r+' are not allowed for a non-existent file (exception raised).

In the tables:

  • Anywhere means that methods IO#rewind, IO#pos=, and IO#seek may be used to change the file’s position, so that allowed reading or writing may occur anywhere in the file.

  • End only means that writing can occur only at end-of-file, and that methods IO#rewind, IO#pos=, and IO#seek do not affect writing.

  • Error means that an exception is raised if disallowed reading or writing is attempted.

Read/Write Modes for Existing File ↑
  • 'r':

    • File is not initially truncated:

      f = File.new('t.txt') # => #<File:t.txt>
      f.size == 0           # => false
      
    • File’s initial read position is 0:

      f.pos # => 0
      
    • File may be read anywhere; see IO#rewind, IO#pos=, IO#seek:

      f.readline # => "First line\n"
      f.readline # => "Second line\n"
      
      f.rewind
      f.readline # => "First line\n"
      
      f.pos = 1
      f.readline # => "irst line\n"
      
      f.seek(1, :CUR)
      f.readline # => "econd line\n"
      
    • Writing is not allowed:

      f.write('foo') # Raises IOError.
      
  • 'w':

    • File is initially truncated:

      path = 't.tmp'
      File.write(path, text)
      f = File.new(path, 'w')
      f.size == 0 # => true
      
    • File’s initial write position is 0:

      f.pos # => 0
      
    • File may be written anywhere (even past end-of-file); see IO#rewind, IO#pos=, IO#seek:

      f.write('foo')
      f.flush
      File.read(path) # => "foo"
      f.pos # => 3
      
      f.write('bar')
      f.flush
      File.read(path) # => "foobar"
      f.pos # => 6
      
      f.rewind
      f.write('baz')
      f.flush
      File.read(path) # => "bazbar"
      f.pos # => 3
      
      f.pos = 3
      f.write('foo')
      f.flush
      File.read(path) # => "bazfoo"
      f.pos # => 6
      
      f.seek(-3, :END)
      f.write('bam')
      f.flush
      File.read(path) # => "bazbam"
      f.pos # => 6
      
      f.pos = 8
      f.write('bah')  # Zero padding as needed.
      f.flush
      File.read(path) # => "bazbam\u0000\u0000bah"
      f.pos # => 11
      
    • Reading is not allowed:

      f.read # Raises IOError.
      
  • 'a':

    • File is not initially truncated:

      path = 't.tmp'
      File.write(path, 'foo')
      f = File.new(path, 'a')
      f.size == 0 # => false
      
    • File’s initial position is 0 (but is ignored):

      f.pos # => 0
      
    • File may be written only at end-of-file; IO#rewind, IO#pos=, IO#seek do not affect writing:

      f.write('bar')
      f.flush
      File.read(path) # => "foobar"
      f.write('baz')
      f.flush
      File.read(path) # => "foobarbaz"
      
      f.rewind
      f.write('bat')
      f.flush
      File.read(path) # => "foobarbazbat"
      
    • Reading is not allowed:

      f.read # Raises IOError.
      
  • 'r+':

    • File is not initially truncated:

      path = 't.tmp'
      File.write(path, text)
      f = File.new(path, 'r+')
      f.size == 0 # => false
      
    • File’s initial read position is 0:

      f.pos # => 0
      
    • File may be read or written anywhere (even past end-of-file); see IO#rewind, IO#pos=, IO#seek:

      f.readline # => "First line\n"
      f.readline # => "Second line\n"
      
      f.rewind
      f.readline # => "First line\n"
      
      f.pos = 1
      f.readline # => "irst line\n"
      
      f.seek(1, :CUR)
      f.readline # => "econd line\n"
      
      f.rewind
      f.write('WWW')
      f.flush
      File.read(path)
      # => "WWWst line\nSecond line\nFourth line\nFifth line\n"
      
      f.pos = 10
      f.write('XXX')
      f.flush
      File.read(path)
      # => "WWWst lineXXXecond line\nFourth line\nFifth line\n"
      
      f.seek(-6, :END)
      # => 0
      f.write('YYY')
      # => 3
      f.flush
      # => #<File:t.tmp>
      File.read(path)
      # => "WWWst lineXXXecond line\nFourth line\nFifth YYYe\n"
      
      f.seek(2, :END)
      f.write('ZZZ') # Zero padding as needed.
      f.flush
      File.read(path)
      # => "WWWst lineXXXecond line\nFourth line\nFifth YYYe\n\u0000\u0000ZZZ"
      
  • 'a+':

    • File is not initially truncated:

      path = 't.tmp'
      File.write(path, 'foo')
      f = File.new(path, 'a+')
      f.size == 0 # => false
      
    • File’s initial read position is 0:

      f.pos # => 0
      
    • File may be written only at end-of-file; IO#rewind, IO#pos=, IO#seek do not affect writing:

      f.write('bar')
      f.flush
      File.read(path)      # => "foobar"
      f.write('baz')
      f.flush
      File.read(path)      # => "foobarbaz"
      
      f.rewind
      f.write('bat')
      f.flush
      File.read(path) # => "foobarbazbat"
      
    • File may be read anywhere; see IO#rewind, IO#pos=, IO#seek:

      f.rewind
      f.read # => "foobarbazbat"
      
      f.pos = 3
      f.read # => "barbazbat"
      
      f.seek(-3, :END)
      f.read # => "bat"
      
Read/Write Modes for File To Be Created ↑

Note that modes 'r' and 'r+' are not allowed for a non-existent file (exception raised).

  • 'w':

    • File’s initial write position is 0:

      path = 't.tmp'
      FileUtils.rm_f(path)
      f = File.new(path, 'w')
      f.pos # => 0
      
    • File may be written anywhere (even past end-of-file); see IO#rewind, IO#pos=, IO#seek:

      f.write('foo')
      f.flush
      File.read(path) # => "foo"
      f.pos # => 3
      
      f.write('bar')
      f.flush
      File.read(path) # => "foobar"
      f.pos # => 6
      
      f.rewind
      f.write('baz')
      f.flush
      File.read(path) # => "bazbar"
      f.pos # => 3
      
      f.pos = 3
      f.write('foo')
      f.flush
      File.read(path) # => "bazfoo"
      f.pos # => 6
      
      f.seek(-3, :END)
      f.write('bam')
      f.flush
      File.read(path) # => "bazbam"
      f.pos # => 6
      
      f.pos = 8
      f.write('bah')  # Zero padding as needed.
      f.flush
      File.read(path) # => "bazbam\u0000\u0000bah"
      f.pos # => 11
      
    • Reading is not allowed:

      f.read # Raises IOError.
      
  • 'a':

    • File’s initial write position is 0:

      path = 't.tmp'
      FileUtils.rm_f(path)
      f = File.new(path, 'a')
      f.pos # => 0
      
    • Writing occurs only at end-of-file:

      f.write('foo')
      f.pos # => 3
      f.write('bar')
      f.pos # => 6
      f.flush
      File.read(path) # => "foobar"
      
      f.rewind
      f.write('baz')
      f.flush
      File.read(path) # => "foobarbaz"
      
    • Reading is not allowed:

      f.read # Raises IOError.
      
  • 'w+':

    • File’s initial position is 0:

      path = 't.tmp'
      FileUtils.rm_f(path)
      f = File.new(path, 'w+')
      f.pos # => 0
      
    • File may be written anywhere (even past end-of-file); see IO#rewind, IO#pos=, IO#seek:

      f.write('foo')
      f.flush
      File.read(path) # => "foo"
      f.pos # => 3
      
      f.write('bar')
      f.flush
      File.read(path) # => "foobar"
      f.pos # => 6
      
      f.rewind
      f.write('baz')
      f.flush
      File.read(path) # => "bazbar"
      f.pos # => 3
      
      f.pos = 3
      f.write('foo')
      f.flush
      File.read(path) # => "bazfoo"
      f.pos # => 6
      
      f.seek(-3, :END)
      f.write('bam')
      f.flush
      File.read(path) # => "bazbam"
      f.pos # => 6
      
      f.pos = 8
      f.write('bah')  # Zero padding as needed.
      f.flush
      File.read(path) # => "bazbam\u0000\u0000bah"
      f.pos # => 11
      
    • File may be read anywhere (even past end-of-file); see IO#rewind, IO#pos=, IO#seek:

      f.rewind
      # => 0
      f.read
      # => "bazbam\u0000\u0000bah"
      
      f.pos = 3
      # => 3
      f.read
      # => "bam\u0000\u0000bah"
      
      f.seek(-3, :END)
      # => 0
      f.read
      # => "bah"
      
  • 'a+':

    • File’s initial write position is 0:

      path = 't.tmp'
      FileUtils.rm_f(path)
      f = File.new(path, 'a+')
      f.pos # => 0
      
    • Writing occurs only at end-of-file:

      f.write('foo')
      f.pos # => 3
      f.write('bar')
      f.pos # => 6
      f.flush
      File.read(path) # => "foobar"
      
      f.rewind
      f.write('baz')
      f.flush
      File.read(path) # => "foobarbaz"
      
    • File may be read anywhere (even past end-of-file); see IO#rewind, IO#pos=, IO#seek:

      f.rewind
      f.read # => "foobarbaz"
      
      f.pos = 3
      f.read # => "barbaz"
      
      f.seek(-3, :END)
      f.read # => "baz"
      
      f.pos = 800
      f.read # => ""
      

Data Mode ↑

To specify whether data is to be treated as text or as binary data, either of the following may be suffixed to any of the string read/write modes above:

  • 't': Text data; sets the default external encoding to Encoding::UTF_8; on Windows, enables conversion between EOL and CRLF and enables interpreting 0x1A as an end-of-file marker.

  • 'b': Binary data; sets the default external encoding to Encoding::ASCII_8BIT; on Windows, suppresses conversion between EOL and CRLF and disables interpreting 0x1A as an end-of-file marker.

If neither is given, the stream defaults to text data.

Examples:

File.new('t.txt', 'rt')
File.new('t.dat', 'rb')

When the data mode is specified, the read/write mode may not be omitted, and the data mode must precede the file-create mode, if given:

File.new('t.dat', 'b')   # Raises an exception.
File.new('t.dat', 'rxb') # Raises an exception.

File-Create Mode ↑

The following may be suffixed to any writable string mode above:

  • 'x': Creates the file if it does not exist; raises an exception if the file exists.

Example:

File.new('t.tmp', 'wx')

When the file-create mode is specified, the read/write mode may not be omitted, and the file-create mode must follow the data mode:

File.new('t.dat', 'x')   # Raises an exception.
File.new('t.dat', 'rxb') # Raises an exception.

Integer Access Modes ↑

When mode is an integer it must be one or more of the following constants, which may be combined by the bitwise OR operator |: