<!--
{
  "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/write(toFile:atomically:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSData(im)writeToFile:atomically:"
  },
  "title" : "write(toFile:atomically:)"
}
-->

# write(toFile:atomically:)

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

```
func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool
```

## Parameters

`path`

The location to which to write the receiver’s bytes. If `path` contains a tilde (~) character, you must expand it with [`expandingTildeInPath`](/documentation/Foundation/NSString/expandingTildeInPath) before invoking this method.

`useAuxiliaryFile`

If <doc://com.apple.documentation/documentation/Swift/true>, the data is written to a backup file, and then—assuming no errors occur—the backup file is renamed to the name specified by `path`; otherwise, the data is written directly to `path`.

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> if the operation succeeds, otherwise <doc://com.apple.documentation/documentation/Swift/false>.

## Discussion

This method may not be appropriate when writing to publicly accessible files. To securely write data to a public location, use [`FileHandle`](/documentation/Foundation/FileHandle) instead. For more information, see [Securing File Operations](https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/RaceConditions.html#//apple_ref/doc/uid/TP40002585-SW9) in [Secure Coding Guide](https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Introduction.html#//apple_ref/doc/uid/TP40002415).

---

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)