<!--
{
  "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/FileManager/createFile(atPath:contents:attributes:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFileManager(im)createFileAtPath:contents:attributes:"
  },
  "title" : "createFile(atPath:contents:attributes:)"
}
-->

# createFile(atPath:contents:attributes:)

Creates a file with the specified content and attributes at the given location.

```
func createFile(atPath path: String, contents data: Data?, attributes attr: [FileAttributeKey : Any]? = nil) -> Bool
```

## Parameters

`path`

The path for the new file.

`data`

A data object containing the contents of the new file.

`attr`

A dictionary containing the attributes to associate with the new file. You can use these attributes to set the owner and group numbers, file permissions, and modification date. For a list of keys, see [`FileAttributeKey`](/documentation/Foundation/FileAttributeKey). If you specify `nil` for `attributes`, the file is created with a set of default attributes.

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> if the operation was successful or if the item already exists, otherwise <doc://com.apple.documentation/documentation/Swift/false>.

## Discussion

If you specify `nil` for the `attributes` parameter, this method uses a default set of values for the owner, group, and permissions of any newly created directories in the path. Similarly, if you omit a specific attribute, the default value is used. The default values for newly created files are as follows:

- Permissions are set according to the umask of the current process. For more information, see umask.
- The owner ID is set to the effective user ID of the process.
- The group ID is set to that of the parent directory.

If a file already exists at `path`, this method overwrites the contents of that file if the current process has the appropriate privileges to do so.

## See Also

[`attributesOfItem(atPath:)`](/documentation/Foundation/FileManager/attributesOfItem(atPath:))

Returns the attributes of the item at a given path.

[`contents(atPath:)`](/documentation/Foundation/FileManager/contents(atPath:))

Returns the contents of the file at the specified path.

[`setAttributes(_:ofItemAtPath:)`](/documentation/Foundation/FileManager/setAttributes(_:ofItemAtPath:))

Sets the attributes of the specified file or directory.

[`FileAttributeKey`](/documentation/Foundation/FileAttributeKey)

Keys in dictionaries used to get and set file attributes.



---

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)