说到文字编码,与文件读写打过交道的软件开发人员应该都知道,比如国际通用:UTF-8编码,ANSCII编码,Unicode编码中文:GB2312,GBK日文,Shift-jis等等。
说了这么多无关紧要的话,技术是来解决实际问题的:
- 如何读取UTF-8编码的文本文件?
- 如何读取GB2312(中文)的文本文件?
- 如何读取其它编码文件?**
首先解决第一个问题,
- 如何读取UTF-8编码的文本文件?
NSString
*filePath = [[[
NSBundle
mainBundle] bundlePath] stringByAppendingPathComponent:fileName];
[
NSString
stringWithContentsOfFile:filePath encoding:
NSUTF8StringEncoding
error:
nil
]
//当然也可以采用如下方法
//NSData *data = [NSData dataWithContentsOfFile:filePath];
//NSString *textFile = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- 如何读取GB2312(中文)的文本文件?
可能很多人会想到,那还不简单,apple肯定提供了gb2312的文件编码常量,让我们看看有那些常量吧,看看 NSUTF8StringEncoding定义文件,应该会有GB2312的常量定义。
/* Note that in addition to the values explicitly listed below, NSStringEncoding supports encodings provided by CFString.
See CFStringEncodingExt.h for a list of these encodings.
See CFString.h for functions which convert between NSStringEncoding and CFStringEncoding.
*/
enum
{
NSASCIIStringEncoding
= 1,
/* 0..127 only */
NSNEXTSTEPStringEncoding
= 2,
NSJapaneseEUCStringEncoding
= 3,
NSUTF8StringEncoding
= 4,
NSISOLatin1StringEncoding
= 5,
NSSymbolStringEncoding
= 6,
NSNonLossyASCIIStringEncoding
= 7,
NSShiftJISStringEncoding
= 8,
/* kCFStringEncodingDOSJapanese */
NSISOLatin2StringEncoding
= 9,
NSUnicodeStringEncoding
= 10,
NSWindowsCP1251StringEncoding
= 11,
/* Cyrillic; same as AdobeStandardCyrillic */
NSWindowsCP1252StringEncoding
= 12,
/* WinLatin1 */
NSWindowsCP1253StringEncoding
= 13,
/* Greek */
NSWindowsCP1254StringEncoding
= 14,
/* Turkish */
NSWindowsCP1250StringEncoding
= 15,
/* WinLatin2 */
NSISO2022JPStringEncoding
= 21,
/* ISO 2022 Japanese encoding for e-mail */
NSMacOSRomanStringEncoding
= 30,
NSUTF16StringEncoding
=
NSUnicodeStringEncoding
,
/* An alias for NSUnicodeStringEncoding */
#if MAC_OS_X_VERSION_10_4 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_2_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
NSUTF16BigEndianStringEncoding
= 0x90000100,
/* NSUTF16StringEncoding encoding with explicit endianness specified */
NSUTF16LittleEndianStringEncoding
= 0x94000100,
/* NSUTF16StringEncoding encoding with explicit endianness specified */
NSUTF32StringEncoding
= 0x8c000100,
NSUTF32BigEndianStringEncoding
= 0x98000100,
/* NSUTF32StringEncoding encoding with explicit endianness specified */
NSUTF32LittleEndianStringEncoding
= 0x9c000100
/* NSUTF32StringEncoding encoding with explicit endianness specified */
#endif
};
很遗憾,我没找到,不过Apple留了个线索让我取找,仔细看看它的注释
Note that in addition to the values explicitly listed below, NSStringEncoding supports encodings provided by CFString.
See CFStringEncodingExt.h for a list of these encodings.
See CFString.h for functions which convert between NSStringEncoding and CFStringEncoding.
我的英文比较不好,但大意是看明白了,不在下面支持了编码格式,在CFStringEncodingExt.h 里头文件申明 。
我们通过finder的文件查找方法找到CFStringEncodingExt.h 。
仔细看看确实找到了kCFStringEncodingGB_18030_2000 ( 我以为是kCFStringEncodingGB_2312_80 ,实际上不是) 但是这个是CFStringEncoding类型,我们需要NSEncode的类型。
CFString 和NSString具有相同的内存结构,也是NSString的重要补充,通过查找CFString的帮助文档,找到了这个方法
CFStringConvertEncodingToNSStringEncoding
NSStringEncoding
enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString
*textFile = [
NSString
stringWithContentsOfFile:filePath encoding:enc error:
nil
];
那么第二个问题就解决了
- 3。如何读取其它编码文件?
相信,通过以上的方法,第三个问题也可以很顺利的解决了。
实际项目中可以先进行UT8解码,不成功再使用GBK,然后再尝试使用GB_18030
+(NSString *)encodeWithURL:(NSURL *)url
{
if (!url) {
return @"";
}
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
if (!content) {
content = [NSString stringWithContentsOfURL:url encoding:0x80000632 error:nil];
}
if (!content) {
content = [NSString stringWithContentsOfURL:url encoding:0x80000631 error:nil];
}
if (!content) {
return @"";
}
return content;
}