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:

remilib/extensions.cr

Class Method Summary

Instance methods inherited from class IO

endBold : self endBold, endColor : self endColor, endConceal : self endConceal, endCrossedOut : self endCrossedOut, endEncircled : self endEncircled, endFaint : self endFaint, endFastBlink : self endFastBlink, endFraktur : self endFraktur, endFramed : self endFramed, endItalic : self endItalic, endOverlined : self endOverlined, endReverseVideo : self endReverseVideo, endSlowBlink : self endSlowBlink, endUnderline : self endUnderline, readBytes(count : Int) : Bytes readBytes, readFloat32 readFloat32, readFloat32BE readFloat32BE, readFloat64 readFloat64, readFloat64BE readFloat64BE, readInt128 readInt128, readInt128BE readInt128BE, readInt16 readInt16, readInt16BE readInt16BE, readInt24 readInt24, readInt24BE readInt24BE, readInt32 readInt32, readInt32BE readInt32BE, readInt64 readInt64, readInt64BE readInt64BE, readInt8 readInt8, readUInt128 readUInt128, readUInt128BE readUInt128BE, readUInt16 readUInt16, readUInt16BE readUInt16BE, readUInt32 readUInt32, readUInt32BE readUInt32BE, readUInt64 readUInt64, readUInt64BE readUInt64BE, readUInt8 readUInt8, startBold : self startBold, startColor(fg : RemiLib::Console::Color, bg : RemiLib::Console::Color = RemiLib::Console::Color::Default) : self startColor, startConceal : self startConceal, startCrossedOut : self startCrossedOut, startEncircled : self startEncircled, startFaint : self startFaint, startFastBlink : self startFastBlink, startFraktur : self startFraktur, startFramed : self startFramed, startItalic : self startItalic, startOverlined : self startOverlined, startReverseVideo : self startReverseVideo, startSlowBlink : self startSlowBlink, startUnderline : self startUnderline, withBold(&) withBold, withColor(fg : RemiLib::Console::Color, bg : RemiLib::Console::Color = RemiLib::Console::Color::Default, &) : self withColor, withConceal(&) withConceal, withCrossedOut(&) withCrossedOut, withEncircled(&) withEncircled, withExcursion(gotoHere, &)
withExcursion(&)
withExcursion
, withFaint(&) withFaint, withFastBlink(&) withFastBlink, withFraktur(&) withFraktur, withFramed(&) withFramed, withItalic(&) withItalic, withOverlined(&) withOverlined, withReverseVideo(&) withReverseVideo, withSlowBlink(&) withSlowBlink, withUnderline(&) withUnderline, writeFloat32(value : Float32) : self writeFloat32, writeFloat32BE(value : Float32) : self writeFloat32BE, writeFloat64(value : Float64) : self writeFloat64, writeFloat64BE(value : Float64) : self writeFloat64BE, writeInt128(value : Int128) : self writeInt128, writeInt128BE(value : Int128) : self writeInt128BE, writeInt16(value : Int16) : self writeInt16, writeInt16BE(value : Int16) : self writeInt16BE, writeInt24(data : Int32) writeInt24, writeInt24BE(data : Int32) writeInt24BE, writeInt32(value : Int32) : self writeInt32, writeInt32BE(value : Int32) : self writeInt32BE, writeInt64(value : Int64) : self writeInt64, writeInt64BE(value : Int64) : self writeInt64BE, writeInt8(value : Int8) : Nil writeInt8, writeUInt128(value : UInt128) : self writeUInt128, writeUInt128BE(value : UInt128) : self writeUInt128BE, writeUInt16(value : UInt16) : self writeUInt16, writeUInt16BE(value : UInt16) : self writeUInt16BE, writeUInt32(value : UInt32) : self writeUInt32, writeUInt32BE(value : UInt32) : self writeUInt32BE, writeUInt64(value : UInt64) : self writeUInt64, writeUInt64BE(value : UInt64) : self writeUInt64BE, writeUInt8(value : UInt8) : Nil writeUInt8

Class methods inherited from class IO

readBytes(io : IO, count : Int) : Bytes readBytes, readFloat32(io : IO) : Float32 readFloat32, readFloat32BE(io : IO) : Float32 readFloat32BE, readFloat64(io : IO) : Float64 readFloat64, readFloat64BE(io : IO) : Float64 readFloat64BE, readInt128(io : IO) : Int128 readInt128, readInt128BE(io : IO) : Int128 readInt128BE, readInt16(io : IO) : Int16 readInt16, readInt16BE(io : IO) : Int16 readInt16BE, readInt24(io : IO) : Int32 readInt24, readInt24BE(io : IO) : Int32 readInt24BE, readInt32(io : IO) : Int32 readInt32, readInt32BE(io : IO) : Int32 readInt32BE, readInt64(io : IO) : Int64 readInt64, readInt64BE(io : IO) : Int64 readInt64BE, readInt8(io : IO) : Int8 readInt8, readUInt128(io : IO) : UInt128 readUInt128, readUInt128BE(io : IO) : UInt128 readUInt128BE, readUInt16(io : IO) : UInt16 readUInt16, readUInt16BE(io : IO) : UInt16 readUInt16BE, readUInt32(io : IO) : UInt32 readUInt32, readUInt32BE(io : IO) : UInt32 readUInt32BE, readUInt64(io : IO) : UInt64 readUInt64, readUInt64BE(io : IO) : UInt64 readUInt64BE, readUInt8(io : IO) : UInt8 readUInt8, writeInt8(io : IO, value : Int8) : Nil writeInt8, writeUInt8(io : IO, value : UInt8) : Nil writeUInt8

Class Method Detail

def self.createEmpty(path : String | Path, size : Int) : Nil #

Creates a file at path that is size bytes large, filled entirely with zeros.


[View source]