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:

crystal-ptools.cr

Constant Summary

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

Class Method Summary

Class Method Detail

def self.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

[View source]
def self.bmp?(file : String | Path) : Bool #

Is the file a bitmap file?


[View source]
def self.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.


[View source]
def self.gif?(file : String | Path) : Bool #

Is the file a gif?


[View source]
def self.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"]


[View source]
def self.ico?(file : String | Path) : Bool #

Is the file an ico file?


[View source]
def self.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)


[View source]
def self.jpg?(file : String | Path) : Bool #

Is the file a jpeg file?


[View source]
def self.png?(file : String | Path) : Bool #

Is the file a png file?


[View source]
def self.readn(filename : Path | String, nbytes : Int, encoding = nil, invalid = nil) : Bytes #

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


[View source]
def self.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"]


[View source]
def self.tiff?(file : String | Path) : Bool #

Is the file a tiff?


[View source]
def self.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.


[View source]
def self.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


[View source]
def self.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


[View source]