File
Undocumented
-
Returns the pathname used to create file as a string. Does not normalize the name.
Declaration
Swift
public let path: String
-
Returns the pathname used to create file as a string. Does not normalize the name. An alias to
File#path
.Declaration
Swift
public var to_path: String
-
Undocumented
-
Opens the file named by filename according to the given mode and returns a new
File
object.Declaration
Swift
public class func new(_ path: String, _ mode: String = "r", closure: ((File) -> ())? = nil) -> File
Parameters
path
A file path.
mode
A string represents the mode to open the file.
closure
If given, file would be closed automatically after closure executed.
Return Value
A file object.
-
Opens the file named by filename according to the given mode and returns a new
File
object.See also
An alias toFile#new
Declaration
Swift
public class func open(_ path: String, _ mode: String = "r", closure: ((File) -> ())? = nil) -> File
Parameters
path
A file path.
mode
A string represents the mode to open the file.
closure
If given, file would be closed automatically after closure executed.
Return Value
A file object.
-
Returns the last component of the filename given in
filename
.File.basename("/home/work/file.swift") #=> "file.swift"
If
suffix
is given and present at the end offilename
, it is removed.File.basename("/home/work/file.swift", ".swift") #=> "file"
If
suffix
is.*
, any extension will be removed.File.basename("/home/work/file.swift", ".*") #=> "file" File.basename("/home/work/file.rb", ".*") #=> "file"
Declaration
Swift
public class func basename(_ filename: String, _ suffix: String = "") -> String
Parameters
filename
A file path string.
suffix
A string will be removed from the filename.
Return Value
The last component with or without extension.
-
Returns all components of the filename given in
filename
except the last one.File.dirname("/home/work/file.swift") #=> "/home/work"
Declaration
Swift
public class func dirname(_ filename: String) -> String
Parameters
filename
A file path string.
Return Value
The directory of given filename.
-
Returns the extension (the portion of file name in path starting from the last period). If path is a dotfile, or starts with a period, then the starting dot is not dealt with the start of the extension.
An empty string will also be returned when the period is the last character in path.
Declaration
Swift
public class func extname(_ path: String) -> String
Parameters
path
A file path.
Return Value
A file extension of empty string.
-
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless
dir
is given, in which case it will be used as the starting point. The given pathname may start with a~
, which expands to the process owner’s home directory (the environment variable HOME must be set correctly).File.expand(path: "~/file.swift") #=> "/absolute/path/to/home/file.swift" File.expand(path: "file.swift", in: "/usr/bin") #=> "/usr/bin/file.swift"
Declaration
Swift
public class func expand(_ path: String, in dir: String? = nil) -> String
Parameters
path
A file path.
dir
A directory path.
Return Value
A absolute path.
-
Converts a pathname to an absolute pathname.
File.absolutePath("~/file.swift") #=> Dir.home + "/file.swift" File.absolutePath("./file.swift") #=> Dir.pwd + "/file.swift"
Declaration
Swift
public class func absolutePath(_ path: String) -> String
Parameters
path
A files path.
Return Value
A absolute path of given file path.
-
Splits the given string into a directory and a file component and returns a tuple with
(File.dirname, File.basename)
.File.split("/home/gumby/.profile") #=> ("/home/gumby", ".profile")
Declaration
Swift
public class func split(_ path: String) -> (String, String)
Parameters
path
A file path.
Return Value
A tuple with a directory and a file component.
-
Returns a new string formed by joining the strings using
/
.File.join("usr", "bin", "swift") #=> "usr/bin/swift"
Declaration
Swift
public class func join(_ paths: String...) -> String
Parameters
paths
An array of file path.
Return Value
A new file path.
-
Returns true if stat is a regular file (not a device file, pipe, socket, etc.).
Declaration
Swift
public class func isFile(_ path: String) -> Bool
Parameters
path
A file path.
Return Value
A bool value.
-
Returns true if the named file is a directory, and false otherwise.
Declaration
Swift
public class func isDirectory(_ path: String) -> Bool
Return Value
A bool value.
-
Returns true if the named file is executable.
File.isExecutable("file.sh")
Declaration
Swift
public class func isExecutable(_ path: String) -> Bool
Parameters
path
A file path.
Return Value
A bool value.
-
Returns true if the named file is readable.
File.isReadable("file.swift")
Declaration
Swift
public class func isReadable(_ path: String) -> Bool
Parameters
path
A file path.
Return Value
A bool value.
-
Returns true if the named file is writable.
File.isWritable("file.rb")
Declaration
Swift
public class func isWritable(_ path: String) -> Bool
Parameters
path
A file path.
Return Value
A bool value.
-
Returns true if the named file is deletable.
File.isDeletable("file.py")
Declaration
Swift
public class func isDeletable(_ path: String) -> Bool
Parameters
path
A file path.
Return Value
A bool value.
-
Returns true if the named file exists, and false otherwise.
File.isExist("file.exs")
Declaration
Swift
public class func isExist(_ path: String) -> Bool
Parameters
path
A file path.
Return Value
A bool value.
-
Changes permission bits on the named file(s) to the bit pattern represented by
mode
.File.chmod(0o777, "file.swift")
Declaration
Swift
public class func chmod(_ mode: Int, _ paths: String...) -> Int
Parameters
mode
A permission bits.
paths
An array of file path.
Return Value
The number of files processed.
-
Returns the file size of the named file.
File.size("somefile") #=> 1234 File.size("emptyfile") #=> 0
Declaration
Swift
public class func size(_ path: String) -> Int
Parameters
path
A file path.
Return Value
The size of file.
-
Returns the last access time for the named file as a
Date
object.Declaration
Swift
public class func atime(_ path: String) -> Date
Parameters
path
A file path.
Return Value
The atime of file.
-
Returns the birth time for the named file.
Declaration
Swift
public class func birthtime(_ path: String) -> Date
Parameters
path
A file path.
Return Value
The birthtime of file.
-
Returns true if the named file is a block device.
Declaration
Swift
public class func isBlockDev(_ path: String) -> Bool
Parameters
path
A file path.
Return Value
A bool value.
-
Returns true if the named file is a character device.
Declaration
Swift
public class func isCharDev(_ path: String) -> Bool
Parameters
path
A file path.
Return Value
A bool value.
-
Deletes the named files, returning the number of names passed as arguments.
Throws
FileManager#removeItem(atPath:)
Declaration
Swift
public class func delete(_ paths: String...) throws -> Int
Parameters
paths
An array of file path.
Return Value
The number of names passed as arguments.
-
Returns true if the named file exists and has a zero size.
Declaration
Swift
public class func isZero(_ path: String) -> Bool
Parameters
path
A file path.
Return Value
A bool value.
-
Identifies the type of the named file; the return string is one of
file
,directory
,characterSpecial
,blockSpecial
,fifo
,link
,socket
, orunknown
.Declaration
Swift
public class func ftype(_ path: String) -> String
Parameters
path
A file path.
Return Value
A string describes the ftype.