Repository navigation
Reading Strings From and Writing Strings To Files and URLs
使用NSString对文件和URL进行读取,你只需要简单地指明资源使用的是什么编码————如果你不知道文件的编码,读取它将会是一个很大的挑战。当你写一个文件或URL时,必须指定写入的编码格式。(尽可能地使用URL,因为写URL是相对高效的)。
NSString 提供多种方法来从文件和URL中读取数据。一般来说,如果你知道文件的编码格式,读取它将会变得更容易。只有文本而不知道它的编码格式,读取将会十分困难。你应该尽量避免这种情况————所有文本读写操作应该指定编码格式(最好使用UTF-8或UTF-16+BOM)。
从已知编码格式的文件和URL中读取,可以使用stringWithContentsOfFile:encoding:error:或stringWithContentsOfURL:encoding:error:或者相应的init...方法,就像下面的例子中使用的:
NSURL *URL = ...;
NSError *error;
NSString *stringFromFileAtURL = [[NSString alloc]
initWithContentsOfURL:URL
encoding:NSUTF8StringEncoding
error:&error];
if (stringFromFileAtURL == nil) {
// an error occurred
NSLog(@"Error reading file at %@\n%@",
URL, [error localizedFailureReason]);
// implementation continues ...同样可以使用一个数据对象(data object)来对一个字符串初始化,就像下面例子中所做的。同样的,你需要指明正确的编码格式。
NSURL *URL = ...;
NSData *data = [NSData dataWithContentsOfURL:URL];
// Assuming data is in UTF8.
NSString *string = [NSString stringWithUTF8String:[data bytes]];
// if data is in another encoding, for example ISO-8859-1
NSString *string = [[NSString alloc]
initWithData:data encoding: NSISOLatin1StringEncoding];如果你发现自己并不知道文本的编码格式,对必然发生的错误,必须要有一种纠错的机制。例如,Apple的邮件(mail)和Safari浏览器都有(选择)编码格式的菜单和允许使用者以一种明确的编码格式重新打开文件的文本编辑器。
如果必须要去猜文本的编码格式(实在没有明确信息来确定编码格式,只能靠猜): 1.试试stringWithContentsOfFile:usedEncoding:error:或initWithContentsOfFile:usedEncoding:error: (或者与URL—based等价的方法)。
2.如果1失败了,试着用指定UTF-8格式去读取源文件。
3.如果2失败了,尝试一种适当的、已有的编码方式。
这里“适当的”要根据具体情况而定:可能是默认的C语言字符编码,也可能是ISO或者windows Latin 1等等,取决于你的源文件的来源。
4.最后,你可以试试Application Kit中的NSAttributedString加载方法(例如:initWithURL:options:documentAttributes:error:);
这些方法尝试去加载文本文件,并返回相应的文件编码格式信息。它们可以或多或少地被使用在任意格式的文档中。如果你的应用对文本(格式)没有特殊的指定(格式),它们是值得考虑的。这些方法或许不适合于被当作基础层次的(Foundation-level)工具和那些不是自然语言的文本。
相比于“文件和URLs的读取”,写入操作显得十分简单直接————NSString提供了两种简便的方法, writeToFile:atomically:encoding:error:和writeToURL:atomically:encoding:error:。你必须指定文件的编码格式,并且选择是否atomically去写源文件。若不选择atomically写源文件,字符串会直接写入你提供的路径源文件中。若选择atomically写,字符串先被写入一个临时的中间文件,然后这个中间文件被放在源文件路径并被重命名为源文件。这个选项保证了,对已存在的文件不会发生写文件死锁问题。如果要对一个URL写操作同时目标是不可atomically写入的(accessed atomically),atomically选项会被忽略。
NSURL *URL = ...;
NSString *string = ...;
NSError *error;
BOOL ok = [string writeToURL:URL atomically:YES
encoding:NSUnicodeStringEncoding error:&error];
if (!ok) {
// an error occurred
NSLog(@"Error writing file at %@\n%@",
path, [error localizedFailureReaso这张表总结了在文件和URL中对字符串对象读写的几种常见方法:
| Source | Creation method | Extraction method |
|---|---|---|
| URL contents | stringWithContentsOfURL:encoding:error: stringWithContentsOfURL:usedEncoding:error: | writeToURL:atomically:encoding:error: |
| File contents | stringWithContentsOfFile:encoding:error: stringWithContentsOfFile:usedEncoding:error: | writeToFile:atomically:encoding:error: |
- Introduction
- Strings
- Creating and Converting String Objects
- Formatting String Objects
- String Format Specifiers
- Reading Strings From and Writing Strings To Files and URLs
- Searching, Comparing, and Sorting Strings
- Words, Paragraphs, and Line Breaks
- Characters and Grapheme Clusters
- Character Sets
- Scanners
- String Representations of File Paths
- Drawing Strings
- Document Revision History
- Index