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

# write(toFile:atomically:)

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

## Declaration

```swift
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 doc://com.apple.foundation/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

Return Value true if the operation succeeds, otherwise false.

## 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.

## See Also

### Writing Data to a File

- [write(toFile:options:)](foundation/nsdata/write(tofile:options:).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)
