---
title: FileHandle
framework: foundation
role: symbol
role_heading: Class
path: foundation/filehandle
---

# FileHandle

An object-oriented wrapper for a file descriptor.

## Declaration

```swift
class FileHandle
```

## Mentioned in

About Apple File System

## Overview

Overview You use file handle objects to access data associated with files, sockets, pipes, and devices. For files, you can read, write, and seek within the file. For sockets, pipes, and devices, you can use a file handle object to monitor the device and process data asynchronously. Most creation methods for FileHandle cause the file handle object to take ownership of the associated file descriptor. This means that the file handle object both creates the file descriptor and is responsible for closing it later, usually when the system deallocates the file handle object. If you want to use a file handle object with a file descriptor that you created, use the init(fileDescriptor:) method or use the init(fileDescriptor:closeOnDealloc:) method and pass false for the flag parameter. Run Loop Considerations When using a file handle object to communicate asynchronously with a socket, you must initiate the corresponding operations from a thread with an active run loop. Although the read, accept, and wait operations themselves are performed asynchronously on background threads, the file handle uses a run loop source to monitor the operations and notify your code appropriately. Therefore, you must call those methods from your application’s main thread or from any thread where you’ve configured a run loop and are using it to process events.

## Topics

### Creating a file handle

- [init(fileDescriptor:)](foundation/filehandle/init(filedescriptor:).md)
- [init(fileDescriptor:closeOnDealloc:)](foundation/filehandle/init(filedescriptor:closeondealloc:).md)
- [init(forReadingAtPath:)](foundation/filehandle/init(forreadingatpath:).md)
- [init(forReadingFromURL:)](foundation/filehandle/init(forreadingfromurl:).md)
- [init(forWritingAtPath:)](foundation/filehandle/init(forwritingatpath:).md)
- [init(forWritingToURL:)](foundation/filehandle/init(forwritingtourl:).md)
- [init(forUpdatingAtPath:)](foundation/filehandle/init(forupdatingatpath:).md)
- [init(forUpdatingURL:)](foundation/filehandle/init(forupdatingurl:).md)
- [init(coder:)](foundation/filehandle/init(coder:).md)

### Getting a file handle

- [standardError](foundation/filehandle/standarderror.md)
- [standardInput](foundation/filehandle/standardinput.md)
- [standardOutput](foundation/filehandle/standardoutput.md)
- [nullDevice](foundation/filehandle/nulldevice.md)

### Getting a file descriptor

- [fileDescriptor](foundation/filehandle/filedescriptor.md)

### Reading from a file handle asynchronously

- [bytes](foundation/filehandle/bytes.md)
- [FileHandle.AsyncBytes](foundation/filehandle/asyncbytes.md)

### Reading from a file handle synchronously

- [availableData](foundation/filehandle/availabledata.md)
- [readToEnd()](foundation/filehandle/readtoend().md)
- [read(upToCount:)](foundation/filehandle/read(uptocount:).md)

### Reading asynchronously with notifications

- [acceptConnectionInBackgroundAndNotify()](foundation/filehandle/acceptconnectioninbackgroundandnotify().md)
- [acceptConnectionInBackgroundAndNotify(forModes:)](foundation/filehandle/acceptconnectioninbackgroundandnotify(formodes:).md)
- [readInBackgroundAndNotify()](foundation/filehandle/readinbackgroundandnotify().md)
- [readInBackgroundAndNotify(forModes:)](foundation/filehandle/readinbackgroundandnotify(formodes:).md)
- [readToEndOfFileInBackgroundAndNotify()](foundation/filehandle/readtoendoffileinbackgroundandnotify().md)
- [readToEndOfFileInBackgroundAndNotify(forModes:)](foundation/filehandle/readtoendoffileinbackgroundandnotify(formodes:).md)
- [waitForDataInBackgroundAndNotify()](foundation/filehandle/waitfordatainbackgroundandnotify().md)
- [waitForDataInBackgroundAndNotify(forModes:)](foundation/filehandle/waitfordatainbackgroundandnotify(formodes:).md)

### Writing to a file handle

- [write(contentsOf:)](foundation/filehandle/write(contentsof:).md)

### Seeking within a file

- [offset()](foundation/filehandle/offset().md)
- [seekToEnd()](foundation/filehandle/seektoend().md)
- [seek(toOffset:)](foundation/filehandle/seek(tooffset:).md)

### Operating on a file

- [close()](foundation/filehandle/close().md)
- [synchronize()](foundation/filehandle/synchronize().md)
- [truncate(atOffset:)](foundation/filehandle/truncate(atoffset:).md)

### Monitoring for readability and writability

- [readabilityHandler](foundation/filehandle/readabilityhandler.md)
- [writeabilityHandler](foundation/filehandle/writeabilityhandler.md)

### Working with constants

- [Keys for Notification UserInfo Dictionary](foundation/keys-for-notification-userinfo-dictionary.md)
- [Exception Names](foundation/exception-names.md)

### Working with notifications

- [NSFileHandleConnectionAccepted](foundation/nsnotification/name-swift.struct/nsfilehandleconnectionaccepted.md)
- [NSFileHandleDataAvailable](foundation/nsnotification/name-swift.struct/nsfilehandledataavailable.md)
- [readCompletionNotification](foundation/filehandle/readcompletionnotification.md)
- [NSFileHandleReadToEndOfFileCompletion](foundation/nsnotification/name-swift.struct/nsfilehandlereadtoendoffilecompletion.md)

### Working with notification messages

- [FileHandle.ConnectionAcceptedMessage](foundation/filehandle/connectionacceptedmessage.md)
- [FileHandle.DataAvailableMessage](foundation/filehandle/dataavailablemessage.md)
- [FileHandle.ReadCompletionMessage](foundation/filehandle/readcompletionmessage.md)
- [FileHandle.ReadToEndOfFileCompletionMessage](foundation/filehandle/readtoendoffilecompletionmessage.md)

### Deprecated

- [readDataToEndOfFile()](foundation/filehandle/readdatatoendoffile().md)
- [readData(ofLength:)](foundation/filehandle/readdata(oflength:).md)
- [write(_:)](foundation/filehandle/write(_:).md)
- [offsetInFile](foundation/filehandle/offsetinfile.md)
- [seekToEndOfFile()](foundation/filehandle/seektoendoffile().md)
- [seek(toFileOffset:)](foundation/filehandle/seek(tofileoffset:).md)
- [closeFile()](foundation/filehandle/closefile().md)
- [synchronizeFile()](foundation/filehandle/synchronizefile().md)
- [truncateFile(atOffset:)](foundation/filehandle/truncatefile(atoffset:).md)
- [NSFileHandleNotificationMonitorModes](foundation/nsfilehandlenotificationmonitormodes.md)

### Initializers

- [init(forReadingFrom:)](foundation/filehandle/init(forreadingfrom:).md)
- [init(forUpdating:)](foundation/filehandle/init(forupdating:).md)
- [init(forWritingTo:)](foundation/filehandle/init(forwritingto:).md)

### Default Implementations

- [FileHandle Implementations](foundation/filehandle/filehandle-implementations.md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [NSSecureCoding](foundation/nssecurecoding.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Managed file access

- [NSFileSecurity](foundation/nsfilesecurity.md)
- [NSFileVersion](foundation/nsfileversion.md)
- [FileWrapper](foundation/filewrapper.md)
