<!--
{
  "availability" : [
    "iOS: 4.0.0 -",
    "iPadOS: 4.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.6.0 -",
    "swift: -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/FileHandle/init(forReadingFromURL:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFileHandle(cm)fileHandleForReadingFromURL:error:::SYNTHESIZED::c:objc(cs)NSFileHandle"
  },
  "title" : "init(forReadingFromURL:)"
}
-->

# init(forReadingFromURL:)

Returns a file handle initialized for reading the file, device, or named socket at the specified URL.

```
convenience init(forReadingFromURL url: URL) throws
```

## Parameters

`url`

The URL of the file, device, or named socket to access.

## Return Value

The initialized file handle object or `nil` if no file exists at `url`.

## Discussion

The file pointer is set to the beginning of the file. You cannot write data to the returned file handle object. Use the [`readDataToEndOfFile()`](/documentation/Foundation/FileHandle/readDataToEndOfFile()) or [`readData(ofLength:)`](/documentation/Foundation/FileHandle/readData(ofLength:)) methods to read data from it.

When using this method to create a file handle object, the file handle owns its associated file descriptor and is responsible for closing it.

> Handling Errors in Swift:
> In Swift, this method returns a nonoptional result 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](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/) and `About Imported Cocoa Error Parameters`.

## See Also

[`availableData`](/documentation/Foundation/FileHandle/availableData)

The data currently available in the receiver.

[`readData(ofLength:)`](/documentation/Foundation/FileHandle/readData(ofLength:))

Reads data synchronously up to the specified number of bytes.

[`readDataToEndOfFile()`](/documentation/Foundation/FileHandle/readDataToEndOfFile())

Reads the available data synchronously up to the end of file or maximum number of bytes.



---

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)