class

File

Inherits Crystal::System::File < IO::FileDescriptor < IO::Buffered < Crystal::System::FileDescriptor < IO < Reference < Object

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:12Z
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

Constants

IMAGE_EXT = [".bmp", ".gif", ".jpg", ".jpeg", ".png", ".ico", ".tiff"] of ::String
PTOOLS_VERSION = "0.1.1"

Class methods

binary?(file : String | Path) : Bool

Returns whether or not +file+ is a binary non-image file, i.e. executable, shared object, etc.

It performs a best guess based on a simple test of the first blksize characters, or 4096, whichever is smaller. If it finds two consecutive zero characters, it is considered binary.

Example:

File.binary?('somefile.exe') # => true
File.binary?('somefile.txt') # => false
Source
bmp?(file : String | Path) : Bool

Is the file a bitmap file?

Source
check_bom?(file)

Returns whether or not the given +text+ contains a BOM marker. If present, we can generally assume it's a text file.

Source
gif?(file : String | Path) : Bool

Is the file a gif?

Source
head(file : String | Path, num_lines : Int = 10)

Reads and returns an array of the first num_lines from file.

Example:

File.head("somefile.txt") # => ["This is line 1", "This is line 2", ...] File.head("somefile.txt", 3) # => ["This is line 1", "This is line 2", "This is line 3"]

Source
ico?(file : String | Path) : Bool

Is the file an ico file?

Source
image?(file : Path | String, check_file_extension : Bool = true) : Bool

Returns whether or not the file is an image. Only JPEG, PNG, BMP, GIF, and ICO are checked against.

This reads and checks the first few bytes of the file. For a version that is more robust, but which depends on a 3rd party C library (and is difficult to build on MS Windows), see the 'filemagic' library.

By default the filename extension is also checked. You can disable this by passing false as the second argument, in which case only the contents are checked.

Examples:

File.image?('somefile.jpg') # => true File.image?('somefile.txt') # => false

The approach I used here is based on information found at http://en.wikipedia.org/wiki/Magic_number_(programming)

Source
jpg?(file : String | Path) : Bool

Is the file a jpeg file?

Source
png?(file : String | Path) : Bool

Is the file a png file?

Source
readn(filename : Path | String, nbytes : Int, encoding = nil, invalid = nil) : Bytes

Read N bytes from path or filename. Returns a Bytes object.

Source
tail(file : String | Path, num_lines : Int = 10, line_size : Int = 1024)

Reads and returns an array of the last num_lines from file. Note that this does not implement tail -f behavior.

Example:

File.tail("somefile.txt") # => ["This is line 10", "This is line 9", ...] File.tail("somefile.txt", 3) # => ["This is line 10", "This is line 9", "This is line 8"]

Source
tiff?(file : String | Path) : Bool

Is the file a tiff?

Source
wc(filename : Path | String, option : String = "words") : Int

Returns a count for the given filename. By default it returns the word count, but you may also specify "chars", "bytes" or "lines" as an option.

Source
whereis(program : String | Path, path : String | Path = ENV["PATH"]) : Array(String) | Nil

Returns an array of each program within path, or nil if it cannot be found. If a path is provided, it should be a string delimited by Process::PATH_DELIMITER. By default your ENV["PATH"] is searched.

Examples:

File.whereis('ruby') # => ['/usr/bin/ruby', '/usr/local/bin/ruby'] File.whereis('foo') # => nil

Source
which(program : String | Path, paths : String | Path = ENV["PATH"]) : String | Nil

Looks for the first occurrence of +program+ within +path+.

On Windows, it looks for executables ending with the suffixes defined in your PATHEXT environment variable, or '.exe', '.bat' and '.com' if that isn't defined, which you may optionally include in +program+.

Returns nil if not found.

Examples:

File.which('ruby') # => '/usr/local/bin/ruby' File.which('foo') # => nil

Source

Nested types