class File
- File
- IO::FileDescriptor
- IO
- Reference
- Object
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
- Crystal::System::File
Defined in:
pathname.crConstructors
Class Method Summary
- .basename(path : Pathname, suffix)
- .basename(path : Pathname)
- .delete(path : Pathname)
- .directory?(path : Pathname)
- .dirname(path : Pathname)
- .each_line(path : Pathname, &)
- .executable?(path : Pathname)
- .exists?(path : Pathname)
- .expand_path(path : Pathname, dir = nil)
- .extname(path : Pathname)
- .file?(path : Pathname)
- .link(old_path : Pathname, new_path : Pathname)
- .link(old_path : Pathname, new_path)
- .link(old_path, new_path : Pathname)
- .lstat(path : Pathname)
- .open(path : Pathname, mode = "r", perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil)
- .open(path : Pathname, mode = "r", perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil, &)
- .read(path : Pathname, encoding = nil, invalid = nil)
- .read_lines(path : Pathname, encoding = nil, invalid = nil)
- .readable?(path : Pathname)
- .real_path(path : Pathname)
- .rename(old_path : Pathname, new_path : Pathname)
- .rename(old_path : Pathname, new_path)
- .rename(old_path, new_path : Pathname)
- .size(path : Pathname)
- .stat(path : Pathname)
- .symlink(old_path : Pathname, new_path : Pathname)
- .symlink(old_path : Pathname, new_path)
- .symlink(old_path, new_path : Pathname)
- .symlink?(path : Pathname)
- .writable?(path : Pathname)
- .write(path : Pathname, content, perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil)
Constructor Detail
def self.new(path : Pathname, mode = "r", perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil)
#
Class Method Detail
def self.open(path : Pathname, mode = "r", perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil)
#
def self.open(path : Pathname, mode = "r", perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil, &)
#