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:

pathname.cr

Constructors

Class Method Summary

Constructor Detail

def self.new(path : Pathname, mode = "r", perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil) #

[View source]

Class Method Detail

def self.basename(path : Pathname, suffix) #

[View source]
def self.basename(path : Pathname) #

[View source]
def self.delete(path : Pathname) #

[View source]
def self.directory?(path : Pathname) #

[View source]
def self.dirname(path : Pathname) #

[View source]
def self.each_line(path : Pathname, &) #

[View source]
def self.executable?(path : Pathname) #

[View source]
def self.exists?(path : Pathname) #

[View source]
def self.expand_path(path : Pathname, dir = nil) #

[View source]
def self.extname(path : Pathname) #

[View source]
def self.file?(path : Pathname) #

[View source]
def self.link(old_path : Pathname, new_path : Pathname) #

[View source]
def self.link(old_path : Pathname, new_path) #

[View source]
def self.link(old_path, new_path : Pathname) #

[View source]
def self.lstat(path : Pathname) #

[View source]
def self.open(path : Pathname, mode = "r", perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil) #

[View source]
def self.open(path : Pathname, mode = "r", perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil, &) #

[View source]
def self.read(path : Pathname, encoding = nil, invalid = nil) #

[View source]
def self.read_lines(path : Pathname, encoding = nil, invalid = nil) #

[View source]
def self.readable?(path : Pathname) #

[View source]
def self.real_path(path : Pathname) #

[View source]
def self.rename(old_path : Pathname, new_path : Pathname) #

[View source]
def self.rename(old_path : Pathname, new_path) #

[View source]
def self.rename(old_path, new_path : Pathname) #

[View source]
def self.size(path : Pathname) #

[View source]
def self.stat(path : Pathname) #

[View source]
def self.symlink(old_path : Pathname, new_path : Pathname) #

[View source]
def self.symlink(old_path : Pathname, new_path) #

[View source]
def self.symlink(old_path, new_path : Pathname) #

[View source]
def self.symlink?(path : Pathname) #

[View source]
def self.writable?(path : Pathname) #

[View source]
def self.write(path : Pathname, content, perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil) #

[View source]