📁 File is a powerful and intuitive file management library for Swift. It simplifies file and folder operations, providing a consistent API across different platforms. File is designed to make working with the file system a breeze, whether you're reading, writing, creating, or deleting files and folders.
File was deployed as Swift Package Manager. Package to install in a project. Add as a dependent item within the swift manifest.
let package = Package(
...
dependencies: [
.package(url: "https://github.com/pelagornis/swift-file", from: "1.1.0")
],
...
)
Then import the File from thr location you want to use.
import File
The documentation for releases and main
are available here:
Path Setting.
let path = Path("/Users/ji-hoonahn/Desktop/") // example
Easy access path.
Path.current
Path.root
Path.library
Path.temporary
Path.home
Path.documents
This example demonstrates how to create a file and write a string to it.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
try file.write("Hello, File!")
This example shows how to write binary data to a file.
import Foundation
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "binary.data")
let data = Data([0x00, 0x01, 0x02, 0x03])
try file.write(data)
This example demonstrates how to append a string to an existing file.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
try file.append(" This is appended content.")
This example shows how to append binary data to an existing file.
import Foundation
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "binary.data")
let data = Data([0x04, 0x05, 0x06, 0x07])
try file.append(data)
This example demonstrates how to read data from a file.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
try file.write("Hello, File!")
let readData = try file.read()
if let readString = String(data: readData, encoding: .utf8) {
print(readString) // Output: Hello, File!
}
If AppKit
is available, this example shows how to open a file using AppKit
.
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
file.open()
#endif
This example shows how to catch and handle FileError
.
import File
let path = Path.root
let filePath = Path("/path/that/do/not/exist/example.txt")
do {
let folder = try Folder(path: path)
let file = try folder.createFile(at: filePath)
try file.write("this will fail")
} catch let error as FileError {
print("FileError: \(error.message)")
if let underlyingError = error.error {
print("Underlying error: \(underlyingError)")
}
} catch {
print("Unexpected error: \(error)")
}
This example demonstrates how to create subfolders and files within a folder.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let subfolder = try folder.createSubfolder(at: "subfolder")
let newFile = try subfolder.createFile(at: "newFile.txt")
try newFile.write("Content of the new file")
This example shows how to retrieve files and subfolders from a folder.
import File
let path = Path.temporary
let folder = try Folder(path: path)
_ = try folder.createSubfolder(at: "subfolder")
_ = try folder.createFile(at: "example.txt")
let files = folder.files
let subfolders = folder.subfolders
print(files) // Output: Contains "example.txt"
print(subfolders) // Output: Contains "subfolder"
This example shows how to move and copy the contents of a folder to another folder.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let destinationFolder = try Folder(path: Path(path.rawValue + "destination"))
_ = try folder.createFile(at: "example.txt")
_ = try folder.createSubfolder(at: "subfolder")
try folder.moveContents(to: destinationFolder)
let files = destinationFolder.files
let subfolders = destinationFolder.subfolders
print(files) // Output: Contains "example.txt"
print(subfolders) // Output: Contains "subfolder"
let copyFolder = try Folder(path: Path(path.rawValue + "copy"))
try destinationFolder.copy(to: copyFolder)
This example shows how to empty a folder (delete all its contents).
import File
let path = Path.temporary
let folder = try Folder(path: path)
_ = try folder.createFile(at: "example.txt")
_ = try folder.createSubfolder(at: "subfolder")
try folder.empty()
This example show how to delete subfolders and file
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
let subfolder = try folder.createSubfolder(at: "subfolder")
try file.delete()
try subfolder.delete()
This example demonstrates how to rename a file or a folder.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "oldName.txt")
try file.rename(to: "newName")
print(file.name) // Output: newName.txt
This example shows how to move a file or a folder to a new location.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let destinationFolder = try Folder(path: Path(path.rawValue + "destination"))
let file = try folder.createFile(at: "example.txt")
try file.move(to: destinationFolder)
This example shows how to copy a file or a folder to a new location.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let destinationFolder = try Folder(path: Path(path.rawValue + "destination"))
let file = try folder.createFile(at: "example.txt")
try file.copy(to: destinationFolder)
This example shows how to delete a file or a folder.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
try file.delete()
swift-file is under MIT license. See the LICENSE file for more info.