zoukankan      html  css  js  c++  java
  • File System Programming --- (一)

    About Files and Directories

    The file system is an important part of any operating system. After all, it’s where users keep their stuff. The organization of the file system plays an important role in helping the user find files. The organization also makes it easier for apps and the system itself to find and access the resources they need to support the user.

    文件系统是任何操作系统的一个重要组成部分。毕竟,它是用户存储数据的地方。文件系统组织在帮助用户查找文件时起着很大的作用。 文件系统组织还让应用程序和系统本身查找和访问支持用户的各种资源变得更加简单。

    This document is intended for developers who are writing software for OS X, iOS, and iCloud. It shows you how to use the system interfaces to access files and directories, move files to and from iCloud, provides guidance on how best to work with files, and it shows you where you should be placing any new files you create.

    该文档面向编写OS X, iOS, 以及iCould软件的开发者。 它告诉你如何使用系统接口来访问文件和文件夹,移动文件到iCloud或从iCloud移到系统,指导如何最好的利用文件,以及显示你应该在哪放置你创建的各种新文件。

    Important: When you adopt App Sandbox in your OS X app, the behavior of many file system features changes. For example, to obtain access to locations outside of your app’s container directory, you must request appropriate entitlements. To obtain persistent access to a file system resource, you must employ the security-scoped bookmark features of the NSURL class or the CFURLRef opaque type. There are changes to the location of app support files (which are relative to your container rather than to the user’s home folder) and to the behavior of Open and Save dialogs (which are presented by Powerbox, not AppKit). For details on all these changes, read App Sandbox Design Guide.

    重要提示:当你在OS X 应用程序里采用应用程序沙盒时,有很多很多文件系统功能将发生改变。比如,要想访问应用程序的容器文件夹之外的地方,你必须要求适当的权限。要想获得对一个文件系统资源的永久访问,你必须采用NSURL类的security-scoped bookmark 功能或CFURLRef opaque 类型。 它们改变应用程序支持文件的位置(它们对应于你的容器而不是用户的home文件夹),以及改变打开和保存对话框的行为(这些行为由Powerbox呈现,而不是由AppKit)。关于所有这些改变的具体信息,请看App Sandbox Design Guide.

    At a Glance(概述)

    To effectively use the file system, know what to expect from the file system itself and which technologies are available for accessing it.

    要想有效地使用文件系统,你需要了解文件系统本身期待什么以及哪些技术能用来访问它。

    The File System Imposes a Specific Organization

    1、文件系统拥有一个特定组织

    The file systems in iOS and OS X are structured to help keep files organized, both for the user and for apps. From the perspective of your code, a well-organized file system makes it easier to locate files your app needs. Of course, you also need to know where you should put any files you create.

    iOS 和 OS X里的文件系统有助于为用户和应用程序保持组织文件。从代码的角度看,一个被良好组织的文件系统让定位应用程序需要的各种文件变得更加容易。 当然,你还需要知道你应该在哪里繁殖你创建的任何文件。

    Access Files Safely

    2、安全地访问文件

    On a multi-user system like OS X, it is possible that more than one app may attempt to read or write a file at the same time as another app is using it. TheNSFileCoordinator and NSFilePresenter classes allow you to maintain file integrity and ensure that if files are made available to other apps (for example, emailing the current TextEdit document) that the most current version is sent.

    在一个像OS X一样的多用户系统中,很可能当一个应用程序在使用一个文件时,有多于一个应用程序在同时尝试读取或写入这个文件。NSFileCoordinator 和 NSFilePresenter 类让你保持文件的完整性并且确保如果这些文件对于其它应用程序是可用的(比如,email当前的文本编辑文档),则发送当前的最新版本。

    How You Access a File Depends on the File Type

    3、访问一个文件时如何依赖文件类型

    Different files require different treatments by your code. For file formats defined by your app, you might read the contents as a binary stream of bytes. For more common file formats, though, iOS and OS X provide higher-level support that make reading and writing those files easier.

    代码对于不同的文件要求有不同的处理方法。 对于应用程序定义的文件格式,你可能通过一个二进制流以字节形式读取它们的内容。 然而,对于更多的常用文件格式,iOS 和 OS X 提供了读取和写入那些文件更简单的各种方法。

    System Interfaces Help You Locate and Manage Your Files

    4、各种系统接口帮你定位和管理文件

    Hard-coded pathnames are fragile and liable to break over time, so the system provides interfaces that search for files in well-known locations. Using these interfaces makes your code more robust and future proof, ensuring that regardless of where files move to, you can still find them.

    硬性编码的路径名是脆弱的,随着时间的推移容易被打破,因此系统提供了在众所周知的位置查找文件的各种接口。使用这些接口让你的代码更加强加和适用于未来,确保不管文件被移到哪里都能被找到。

    Users Interact with Files Using the Standard System Panels

    5、用户使用标准系统面板跟文件交互

    For files that the user creates and manages, your code can use the standard Open and Save panels to ask for the locations of those files. The standard panels present the user with a navigable version of the file system that matches what the Finder presents. You can use these panels without customization, modify the default behavior, or augment them with your own custom content. Even sandboxed apps can use the panels to access files because they work with the underlying security layer to allow exceptions for files outside of your sandbox that the user explicitly chooses.

    对于用户创建和管理的各种文件,代码可以使用标准打开和保存面板来查询那些文件的饿位置。标准面板为用户提供了一个文件系统的通航版本,该版本跟Finder的呈现相匹配。你不需要定制,修改这些默认行为,或用你自定义的内容添加它们就可以使用这些面板。甚至于沙盒应用程序也能使用这些面板来访问文件,因为它们和底层安全层一起允许用户明确选择的沙盒以为的文件成为例外。

    Read and Write Files Asynchronously

    6、异步读取和写入文件

    Because file operations require accessing a disk or network server, you should always access files from a secondary thread of your app. Some of the technologies for reading and writing files run asynchronously without you having to do anything, while others require you to provide your own execution context. All of the technologies do essentially the same thing but offer different levels of simplicity and flexibility.

    因为文件操作要求访问一个磁盘或网络服务器,所以你应该总是从应用程序的辅助线程里访问文件。一个异步读取和写入文件的技术不需要你做任何事情,但是其它技术则要求你提供自己的执行上下文。所有的这些技术都几乎做同样的事情,但是提供了不同层次的简单性和灵活性。

    As you read and write files, you should also use file coordinators to ensure that any actions you take on a file do not cause problems for other apps that care about the file.

    当你读取和写入文件时,你还应该使用file coordinators 来确保你对一个文件所做的任何操作都不会给其它关心该文件的应用程序产生问题。

    Move, Copy, Delete, and Manage Files Like the Finder

    7、像Finder一样移动,拷贝,删除,以及管理文件

    The system interfaces support all of the same types of behaviors that the Finder supports and many more. You can move, copy, create, and delete files and directories just like the user can. Using the programmatic interfaces, you can also iterate through the contents of directories and work with invisible files much more efficiently. More importantly, most of the work can be done asynchronously to avoid blocking your app’s main thread.

    系统接口支持Finder支持的所有同类型的行为,甚至更多。你可以像用户一样移动,拷贝,创建以及删除文件和文件夹。使用程序接口,你还可以遍历文件夹内容以及更有效地与隐藏文件一起工作。更重要的是,大多数工作能够异步完成以避免阻塞应用程序的主线程。

    Optimize Your File-Related Operations

    8、优化文件相关的各种操作

    The file system is one of the slowest parts of a computer so writing efficient code is important. Optimizing your file-system code is about minimizing the work you do and making sure that the operations you do perform are done efficiently.

    文件系统是一个电脑最慢的一个部分之一,所以编写有效的代码很重要。优化文件系统代码是减少你的工作并确保你所做的操作能被有效地执行。

    Relevant chapter: “Performance Tips”

    相关章节:“Performance Tips”

    See Also

    For information about more advanced file-system tasks that you can perform, seeFile System Advanced Programming Topics.

    关于你可以执行的更多高级文件系统任务的信息,请看File System Advanced Programming Topics.

  • 相关阅读:
    交叉编译GDB
    交叉编译Python
    DEBUG memory问题的一些工具以及注意事项
    [qemu] QEMU虚拟机的安装步骤
    分享一个public domain 的大数运算库,可以用于RSA加密解密
    [KERNEL] 在procfs中新增文件的方法
    [backtrace]在不改变代码的情况下,打印可执行文件的backtrace
    【幻化万千戏红尘】qianfeng-JAVA课程结束总结
    【幻化万千戏红尘】qianfengDay30-正则表达式、注解注释基础学习:
    【幻化万千戏红尘】qianfengDay29-Json解析、Gson解析、FASTJSON解析基础学习:
  • 原文地址:https://www.cnblogs.com/patientAndPersist/p/3243316.html
Copyright © 2011-2022 走看看