---
title: "init(contentsOfFile:options:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/nsdata/init(contentsoffile:options:)"
---

# init(contentsOfFile:options:)

Initializes a data object with the content of the file at a given path.

## Declaration

```swift
init(contentsOfFile path: String, options readOptionsMask: NSData.ReadingOptions = []) throws
```

## Parameters

- `path`: The absolute path of the file from which to read data.
- `readOptionsMask`: A mask that specifies options for reading the data. Constant components are described in doc://com.apple.foundation/documentation/Foundation/NSData/ReadingOptions.

## Return Value

Return Value A data object initialized by reading into it the data from the file specified by path.

## Discussion

Discussion note: In Swift, this API is imported as an initializer and is marked with the throws keyword to indicate that it throws an error in cases of failure. You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.

## See Also

### Reading Data from a File

- [init(contentsOfFile:)](foundation/nsdata/init(contentsoffile:).md)
- [NSData.ReadingOptions](foundation/nsdata/readingoptions.md)
- [init(contentsOfMappedFile:)](foundation/nsdata/init(contentsofmappedfile:).md)
- [dataWithContentsOfMappedFile(_:)](foundation/nsdata/datawithcontentsofmappedfile(_:).md)
