<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSData/dataWithContentsOfFile:options:error:",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSData(cm)dataWithContentsOfFile:options:error:"
  },
  "title" : "dataWithContentsOfFile:options:error:"
}
-->

# dataWithContentsOfFile:options:error:

Creates a data object by reading every byte from the file at a given path.

```
+ (instancetype) dataWithContentsOfFile:(NSString *) path options:(NSDataReadingOptions) readOptionsMask error:(NSError **) errorPtr;
```

## 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 [`NSData.ReadingOptions`](/documentation/Foundation/NSData/ReadingOptions).

`errorPtr`

If an error occurs, upon return contains an error object that describes the problem.

## Discussion

This method returns `nil` if the data object could not be created. In this case, `errorPtr` will contain an [`NSError`](/documentation/Foundation/NSError) indicating the problem.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)