Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a file, write data into it and read data from it on iOS?
Being a software developer we should be always aware of how to play with files, write to a file, read from the file and so on.
In this post we are going to learn the same, we will be creating a file and writing the data to the file and later reading through the same file.
So let’s get started,
Step 1 − Create new Xcode Project → Single View Application → name it “ReadingWritingFile”
Step 2 − Open ViewController.swift and add new function as show below
public func createAndWriteFile() {
}
Now we will create a file and will print the path of the file.
Step 3 − Inside createAndWriteFile function add
let fileName = "sample"
let documentDirectoryUrl = try! FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true
)
let fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt")
// prints the file path
print("File path \(fileUrl.path)")
Now your createAndWriteFile function should look like below one
public func createAndWriteFile() {
let fileName = "sample"
let documentDirectoryUrl = try! FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true
)
let fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt")
// prints the file path
print("File path \(fileUrl.path)")
//data to write in file.
let stringData = "Hello Tutorials Point"
do {
try stringData.write(to: fileUrl, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print (error)
}
}
Now we will be writing to the file.
Add below code to the existing function
//data to write in file.
let stringData = "Hello Tutorials Point"
do {
try stringData.write(to: fileUrl, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print (error)
}
Step 4 − Your final function should look like below
// function to create file and write into the same.
public func createAndWriteFile() {
let fileName = "sample"
let documentDirectoryUrl = try! FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true
)
let fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt")
// prints the file path
print("File path \(fileUrl.path)")
//data to write in file.
let stringData = "Hello Tutorials Point"
do {
try stringData.write(to: fileUrl, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print (error)
}
}
Step 5 − Run the project by calling the new method from viewDidLoad() and navigate the file path and validate the content.
Step 6 − Now we’re going to read the content, copy the below code in the same function
var readFile = ""
do {
readFile = try String(contentsOf: fileUrl)
} catch let error as NSError {
print(error)
}
print (readFile)
And you’re done,

Step 7 − Complete code,
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.createReadAndWriteFile()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// function to create file and write into the same.
public func createReadAndWriteFile() {
let fileName = "sample"
let documentDirectoryUrl = try! FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true
)
let fileUrl = documentDirectoryUrl.appendingPathComponent(fileName).appendingPathExtension("txt")
// prints the file path
print("File path \(fileUrl.path)")
//data to write in file.
let stringData = "Hello Tutorials Point."
do {
try stringData.write(to: fileUrl, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print (error)
}
var readFile = ""
do {
readFile = try String(contentsOf: fileUrl)
} catch let error as NSError {
print(error)
}
print (readFile)
}
}