---
title: "init(contentsOfFile:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/nsstring/init(contentsoffile:)"
---

# init(contentsOfFile:)

Initializes the receiver, a newly allocated NSString object, by reading data from the file named by path.

## Declaration

```swift
convenience init?(contentsOfFile path: String)
```

## Discussion

Discussion Initializes the receiver, a newly allocated NSString object, by reading data from the file named by path. If the contents begin with a byte-order mark (U+FEFF or U+FFFE), interprets the contents as UTF-16 code units; otherwise interprets the contents as data in the default C string encoding. Returns an initialized object, which might be different from the original receiver, or nil if the file can’t be opened.

## See Also

### Related Documentation

- [init(contentsOfFile:encoding:)](foundation/nsstring/init(contentsoffile:encoding:).md)
- [init(contentsOfFile:usedEncoding:)](foundation/nsstring/init(contentsoffile:usedencoding:).md)

### Deprecated

- [string(withCString:)](foundation/nsstring/string(withcstring:).md)
- [init(CString:)](foundation/nsstring/init(cstring:)-vkuo.md)
- [string(withCString:length:)](foundation/nsstring/string(withcstring:length:).md)
- [init(CString:length:)](foundation/nsstring/init(cstring:length:)-5ure3.md)
- [init(CStringNoCopy:length:freeWhenDone:)](foundation/nsstring/init(cstringnocopy:length:freewhendone:)-86dm2.md)
- [string(withContentsOfFile:)](foundation/nsstring/string(withcontentsoffile:).md)
- [string(withContentsOf:)](foundation/nsstring/string(withcontentsof:).md)
- [init(contentsOfURL:)](foundation/nsstring/init(contentsofurl:).md)
- [write(toFile:atomically:)](foundation/nsstring/write(tofile:atomically:).md)
- [write(to:atomically:)](foundation/nsstring/write(to:atomically:).md)
- [getCharacters(_:)](foundation/nsstring/getcharacters(_:).md)
- [cString()](foundation/nsstring/cstring().md)
- [lossyCString()](foundation/nsstring/lossycstring().md)
- [cStringLength()](foundation/nsstring/cstringlength().md)
- [getCString(_:)](foundation/nsstring/getcstring(_:).md)
