20200917--iOS之FileHandle,文件操作类

FileHandle是iOS中用于访问文件、套接字、管道和设备数据的类。它可以进行读取、写入、查找等操作,并支持异步通信。通过不同的初始化方法可以获取针对不同路径或URL的文件句柄,用于读写操作。注意,当使用FileHandle进行异步通信时,必须在有活跃Run Loop的线程中进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Class

FileHandle

An object-oriented wrapper for a file descriptor.

 --文件描述符的面向对象的包装者

--所以文件句柄就是对文件描述符的包装者

Declaration

class FileHandle : NSObject

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 file handle object itself is deallocated. 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.

        --FileHandle的大多数创建方法都会导致文件句柄对象取得相关文件描述符的所有权。 这意味着文件句柄对象既创建文件描述符,又负责稍后关闭它,通常是在文件句柄对象本身被释放时。 如果要将文件句柄对象与您创建的文件描述符一起使用,请使用init(fileDescriptor:)方法或使用init(fileDescriptor:closeOnDealloc:)方法并为flag参数传递false。

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 have configured a run loop and are using it to process events.

         --使用文件句柄对象与套接字异步通信时,必须具有活动Run循环的线程启动相应的操作。 尽管读取、接受和等待操作本身在后台线程上异步执行,但文件句柄使用Run循环源来监视操作并适当地通知您的代码。 因此,您必须从 应用程序的主线程 或 已配置Run循环的(并使用它来处理事件的)线程中 调用这些方法。

For more information about configuring and using run loops, see Threading Programming Guide.

        --有关配置和使用运行循环的更多信息,请参见Threading Programming Guide

 

Topics

Getting a File Handle                  --获取文件句柄

init?(forReadingAtPath: String)

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

init(forReadingFrom: URL)

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

init?(forWritingAtPath: String)

Returns a file handle initialized for writing to the file, device, or named socket at the specified path.

init(forWritingTo: URL)

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

init?(forUpdatingAtPath: String)

Returns a file handle initialized for reading and writing to the file, device, or named socket at the specified path.

init(forUpdating: URL)

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

class var standardError: FileHandle

Returns the file handle associated with the standard error file.

class var standardInput: FileHandle

Returns the file handle associated with the standard input file.

class var standardOutput: FileHandle

Returns the file handle associated with the standard output file.

class var nullDevice: FileHandle

Returns a file handle associated with a null device.

 

Creating a File Handle                 --创建文件句柄

init(fileDescriptor: Int32)

Initializes and returns a file handle object associated with the specified file descriptor.

init(fileDescriptor: Int32, closeOnDealloc: Bool)

Initializes and returns a file handle object associated with the specified file descriptor and deallocation policy.

 

Getting a File Descriptor                 --获取文件描述符

var fileDescriptor: Int32

The POSIX file descriptor associated with the receiver.

 

Reading from a File Handle                  --从文件句柄中读数据

var availableData: Data

The data currently available in the receiver.

func readDataToEndOfFile() -> Data

Synchronously reads the available data up to the end of file or maximum number of bytes.

Deprecated

func readData(ofLength: Int) -> Data

Synchronously reads data up to the specified number of bytes.

Deprecated

 

Writing to a File Handle                 --往文件句柄中写数据

func write(Data)

Synchronously writes the specified data to the receiver.

Deprecated

 

Reading and Writing Using Blocks                 --使用闭包读写数据

var readabilityHandler: ((FileHandle) -> Void)?

The block to use for reading the contents of the file handle asynchronously.

var writeabilityHandler: ((FileHandle) -> Void)?

The block to use for writing the contents of the file handle asynchronously.

 

Communicating Asynchronously                 --异步通信

func acceptConnectionInBackgroundAndNotify()

Accepts a socket connection (for stream-type sockets only) in the background and creates a file handle for the “near” (client) end of the communications channel.

func acceptConnectionInBackgroundAndNotify(forModes: [RunLoop.Mode]?)

Accepts a socket connection (for stream-type sockets only) in the background and creates a file handle for the “near” (client) end of the communications channel.

func readInBackgroundAndNotify()

Reads from the file or communications channel in the background and posts a notification when finished.

func readInBackgroundAndNotify(forModes: [RunLoop.Mode]?)

Reads from the file or communications channel in the background and posts a notification when finished.

func readToEndOfFileInBackgroundAndNotify()

Reads to the end of file from the file or communications channel in the background and posts a notification when finished.

func readToEndOfFileInBackgroundAndNotify(forModes: [RunLoop.Mode]?)

Reads to the end of file from the file or communications channel in the background and posts a notification when finished.

func waitForDataInBackgroundAndNotify()

Asynchronously checks to see if data is available.

func waitForDataInBackgroundAndNotify(forModes: [RunLoop.Mode]?)

Asynchronously checks to see if data is available.

 

Seeking Within a File                 --在文件中查找

var offsetInFile: UInt64

The position of the file pointer within the file represented by the receiver.

Deprecatedfunc seekToEndOfFile() -> UInt64

Puts the file pointer at the end of the file referenced by the receiver and returns the new file offset.

Deprecatedfunc seek(toFileOffset: UInt64)

Moves the file pointer to the specified offset within the file represented by the receiver.

Deprecated

 

Operating on a File                 --对文件进行操作

func closeFile()

Disallows further access to the represented file or communications channel and signals end of file on communications channels that permit writing.

Deprecatedfunc synchronizeFile()

Causes all in-memory data and attributes of the file represented by the receiver to be written to permanent storage.

Deprecatedfunc truncateFile(atOffset: UInt64)

Truncates or extends the file represented by the receiver to a specified offset within the file and puts the file pointer at that position.

Deprecated

 

Constants                 --常量

 

Keys for Notification UserInfo Dictionary

Strings that are used as keys in a userinfo dictionary in a file handle notification.

 

Exception Names

Constant that defines the name of a file operation exception.

 

Notifications                 --通知

NSFileHandle posts several notifications related to asynchronous background I/O operations. They are set to post when the run loop of the thread that started the asynchronous operation is idle.

static let NSFileHandleConnectionAccepted: NSNotification.Name

This notification is posted when an NSFileHandle object establishes a socket connection between two processes, creates an NSFileHandle object for one end of the connection, and makes this object available to observers by putting it in the userInfo dictionary.

static let NSFileHandleDataAvailable: NSNotification.Name

This notification is posted when the file handle determines that data is currently available for reading in a file or at a communications channel.

class let readCompletionNotification: NSNotification.Name

This notification is posted when the file handle reads the data currently available in a file or at a communications channel.

static let NSFileHandleReadToEndOfFileCompletion: NSNotification.Name

This notification is posted when the file handle reads all data in the file or, if a communications channel, until the other process signals the end of data.

Initializers

init?(coder: NSCoder)

 

Instance Methods                 --实例方法

func close()

func offset() -> UInt64

func read(upToCount: Int) -> Data?

func readToEnd() -> Data?

func seek(toOffset: UInt64)

func seekToEnd() -> UInt64

func synchronize()

func truncate(atOffset: UInt64)

func write<T>(contentsOf: T)

 

Relationships                 --继承关系

Inherits From

Conforms To

See Also

Managed File Access

class NSFileSecurity

A stub class that encapsulates security information about a file.

class NSFileVersion

A snapshot of a file at a specific point in time.

class FileWrapper

A representation of a node (a file, directory, or symbolic link) in the file system.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值