#!/bin/bash
touch the/path/to/file
#!/bin/swift
if FileManager.default.fileExists(atPath: "the/path/to/file") {
fputs("file existed", stderr)
}else{
try "".write(to: URL(fileURLWithPath: "123"),
atomically: false,
encoding: .utf8)
}
#!/bin/bash
echo 'String' > the/path/to/file #Creat and add 'String' into it
echo 'Second String' >> the/path/to/file
echo 'Replaced String' > the/path/to/file
#!/bin/swift
let dataToWrite:Date = ...
try dataToWrite.write(to URL(fileURLWithPath: "the/path/to/file"))
// appending a.k.a. shell(>>)
let handle = try FileHandle(forWritingTo: URL(fileURLWithPath: path))
handle.seekToEndOfFile()
handle.write(data)
handle.closeFile()
#!/bin/bash
cat the/path/to/file
#!/bin/swift
let data = try Data(contentsOf: URL(fileURLWithPath: "the/path/to/file"))
#!/bin/bash
rm the/path/to/file
#!/bin/swift
try fileManager.removeItem(atPath: "the/path/to/file")
I highly recommend using Files, which is really good!