使用NSFileHandle创建UTF8文本文件时出现问题 [英] Problem creating UTF8 text file with NSFileHandle

查看:116
本文介绍了使用NSFileHandle创建UTF8文本文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 NSFileHandle 来编写大型文本文件,以避免在内存中处理非常大的 NSString 。我有一个问题,在创建文件并在文本编辑应用程序(Mac)中打开它后,它不正确地显示Unicode字符。如果我使用 NSString writeToFile:atomically:encoding:error:方法将相同的文本写入文件,正确显示一切。

I want to use NSFileHandle to write large text files to avoid handling very large NSString's in memory. I'm having a problem where after creating the file and opening it in the Text Edit app (Mac), it is not displaying the unicode characters correctly. If I write the same text to a file using the NSString writeToFile:atomically:encoding:error: method, Text Edit display everything correctly.

我打开文本编辑中的两个文件,将打开文件编码选项设置为自动,所以我不知道为什么一个工作,另一个方法没有。是否有某种形式的头声明格式是UTF8?

I'm opening both the files in Text Edit with the "opening files encoding" option set to automatic, so I'm not sure why one works and the other method doesn't. Is there some form of header to declare the format is UTF8?

// Standard string
NSString *myString = @"This is a test with a star character \u272d";

// This works fine
// Displays: "This is a test with a star character ✭" in Text Edit
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding];

// This doesn't work
// Displays: "This is a test with a star character ‚ú≠" in Text Edit
[fileManager createFileAtPath:path contents:nil attributes:nil];
fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
[fileHandle writeData:[myString dataUsingEncoding:NSUTF8StringEncoding]];


推荐答案

问题不在于你的代码, :它不尝试将文件解码为UTF-8,除非它有一个UTF-8 BOM标识它。推测,您的代码的第一个版本添加了这样的BOM。有关进一步讨论,请参见此问题

The problem is not with your code, but with Text It doesn't try to decode the file as UTF-8 unless it has a UTF-8 BOM identifying it as such. Presumably, the first version of your code adds such a BOM. See this question for further discussion.

UTF-8数据通常不应包含BOM,因此您可能不应该从第二个版本修改它的代码 - 它正常工作。如果在TextEdit 中打开文件工作,你应该能够在开始时显式地加入BOM( \\\ 的字符串,但是,你不应该这样做,除非你真的需要。

UTF-8 data generally should not include a BOM, so you probably shouldn't modify your code from the second version at all—it's working correctly. If opening the file in TextEdit has to work, you should be able to force the BOM by including it (\ufeff) explicitly at the start of the string, but, again, you should not do that unless you really need to.

这篇关于使用NSFileHandle创建UTF8文本文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆