---
title: "write(toFile:options:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsdata/write(tofile:options:)"
---

# write(toFile:options:)

Writes the data object’s bytes to the file specified by a given path.

## Declaration

```swift
func write(toFile path: String, options writeOptionsMask: NSData.WritingOptions = []) throws
```

## Parameters

- `path`: The location to which to write the receiver’s bytes.
- `writeOptionsMask`: A mask that specifies options for writing the data. Constant components are described in doc://com.apple.foundation/documentation/Foundation/NSData/WritingOptions.

## Discussion

Discussion This method may not be appropriate when writing to publicly accessible files. To securely write data to a public location, use FileHandle instead. For more information, see Securing File Operations in Secure Coding Guide. note: In Swift, this method returns Void 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

### Writing Data to a File

- [write(toFile:atomically:)](foundation/nsdata/write(tofile:atomically:).md)
- [write(to:atomically:)](foundation/nsdata/write(to:atomically:).md)
- [write(to:options:)](foundation/nsdata/write(to:options:).md)
- [NSData.WritingOptions](foundation/nsdata/writingoptions.md)
