class File

Overview

A File instance represents a file entry in the local file system and allows using it as an IO.

file = File.new("path/to/file")
content = file.gets_to_end
file.close

# Implicit close with `open` and a block:
content = File.open("path/to/file") do |file|
  file.gets_to_end
end

# Shortcut of the above:
content = File.read("path/to/file")

# Write to a file by opening with a "write mode" specified.
File.open("path/to/file", "w") do |file|
  file.print "hello"
end
# Content of file on disk will now be "hello".

# Shortcut of the above:
File.write("path/to/file", "hello")

See new for various options mode can be.

Temporary Files

Every tempfile is operated as a File, including initializing, reading and writing.

tempfile = File.tempfile("foo")

File.size(tempfile.path)                   # => 6
File.info(tempfile.path).modification_time # => 2015-10-20 13:11:12 UTC
File.exists?(tempfile.path)                # => true
File.read_lines(tempfile.path)             # => ["foobar"]

Files created from tempfile are stored in a directory that handles temporary files (Dir.tempdir):

File.tempfile("foo").path # => "/tmp/foo.ulBCPS"

It is encouraged to delete a tempfile after using it, which ensures they are not left behind in your filesystem until garbage collected.

tempfile = File.tempfile("foo")
tempfile.delete

Included Modules

Defined in:

atomic_write.cr

Class Method Summary

Class Method Detail

def self.atomic_copy(src : String, dst : String, perm = DEFAULT_CREATE_PERMISSIONS) : Nil #

Ensures the copied file is written completely or not at all preventing corruption of the file.


[View source]
def self.atomic_replace(path : String, perm = DEFAULT_CREATE_PERMISSIONS, encoding = nil, invalid = nil, &block : IO::FileDescriptor, IO::FileDescriptor -> Nil) : Nil #

Allows reading from the original file and atomically replacing using #atomic_write.


[View source]
def self.atomic_write(path : Path | String, perm = DEFAULT_CREATE_PERMISSIONS, encoding = nil, invalid = nil, *, append : Bool = false, &block : IO::FileDescriptor -> Nil) : Nil #

Ensures the content written to the file descriptor is written completely or not at all preventing corruption of the file.

If a file is being created, its initial permissions may be set using the perm parameter. Then the given block will be passed the opened file descriptor as an argument, the file will be automatically closed and saved when the block returns.

This is done by saving the new contents at temporary path. When the new content is successfully written the temporary path is changed to the provided path ensuring the data is not corrupted. If the write fails the temporary file is deleted.


[View source]
def self.atomic_write(path : String, content, perm = DEFAULT_CREATE_PERMISSIONS, encoding = nil, invalid = nil) : Nil #

Writes the provided content completely or not at all preventing file corruption.

This is preformed in the same way as .atomic_write with block.

NOTE If the content is a Slice(UInt8), those bytes will be written. If it's an IO, all bytes from the IO will be written. Otherwise, the string representation of content will be written (the result of invoking to_s on content).


[View source]