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.

 

File System Programming --- (一)的更多相关文章

  1. File System Programming --- (二)

    File System Basics The file systems in OS X and iOS handle the persistent storage of data files, app ...

  2. 谷歌三大核心技术(一)The Google File System中文版

    谷歌三大核心技术(一)The Google File System中文版  The Google File System中文版 译者:alex 摘要 我们设计并实现了Google GFS文件系统,一个 ...

  3. Storage System and File System Courses

    I researched a lot about storage system classes given at good universities this year. This had two r ...

  4. HDFS分布式文件系统(The Hadoop Distributed File System)

    The Hadoop Distributed File System (HDFS) is designed to store very large data sets reliably, and to ...

  5. Common Internet File System

    CIFS (Common Internet File System) is a protocol that gained popularity around the year 2000, as ven ...

  6. Low-overhead enhancement of reliability of journaled file system using solid state storage and de-duplication

    A mechanism is provided in a data processing system for reliable asynchronous solid-state device bas ...

  7. Google File System中文版

    英文原文地址: Google File system 译文原文地址: The Google File System中文版 Google File System中文版 摘要 我们设计并实现了Google ...

  8. The Google File System论文拜读

    The Google File System Sanjay Ghemawat, Howard Gobioff, and Shun-Tak Leung Google∗ 摘要 我们设计并实现了谷歌文件系统 ...

  9. File System Programming---(三)

    Accessing Files and Directories Before you can open a file, you first have to locate it in the file ...

随机推荐

  1. 有关不同浏览器不同版本号的css以及js计算高度的问题

    1.input在全部浏览器以及全部的版本号中,都是定义了高度.然后再定义padding或者border值.这时候都会撑开.高度是本身的高度在padding值和border值 2.select在gool ...

  2. Collection接口和Collections类的简单区别和讲解

    这里仅仅进行一些简单的比较,如果你想要更加详细的信息话,请自己百度. 1.Collection: 是集合类的上层接口.本身是一个Interface,里面包含了一些集合的基本操作. Collection ...

  3. C语言连接MySQL(codeblocks)

    #include <stdio.h> #include <winsock2.h> #include <mysql.h> /*数据库连接用宏*/ #define HO ...

  4. UUID GUID

    http://baike.baidu.com/link?url=xkck9gR5bzOx0oBKP1qNJwGGq3IO56V4i8cg9zTSpSDMVBMA0F7jr0AdkQTGyk7F0FGj ...

  5. Firefox OS开发指南

    在海外社区Leanpub上线了<Firefox OS App开发>高速指南,指引开发人员尝试新技术. 这款<Firefox OS App开发>高速指南现已开放下载.HTML5开 ...

  6. hibernate工作原理及作用

    转载自 http://www.cnblogs.com/dashi/p/3597969.html#commentform JAVA Hibernate工作原理及为什么要用 hibernate 简介:hi ...

  7. Python开发【2.3 模块】

    1.模块导入 import 模块名 from 模块名 import 函数/类/变量 2.模块路径 import sys sys.path 3.模块重新导入 Python3若想在同一次会话中再次运行文件 ...

  8. Marlin固件之—:基础入门与測试

    一.Marlin的简介 Marlin固件是一个3D打印的开源固件,3D打印固件有很多,Marlin最为健全和强大,当然相对也会复杂一些.使用Gcode控制爱.Gcode是数控机床等工控控制使用范围较广 ...

  9. java的nio包的SelectionKey,Selector,SelectableChannel三者的缠绵关系概述

    猛击这里 java的nio包的SelectionKey,Selector,SelectableChannel三者的缠绵关系概述

  10. VUE 之 JS指令

    1.v-text的用法: 2.v-html 3.v-for 4.v-if , v-else if ,v-else v-if 每次生成都只有一个标签,即符合条件的标签. 5.v-show v-show ...