iOS 判断文件编码格式

        @autoreleasepool {
            
            NSArray *arrEncoding = @[@(NSASCIIStringEncoding),
                                     @(NSNEXTSTEPStringEncoding),
                                     @(NSJapaneseEUCStringEncoding),
                                     @(NSUTF8StringEncoding),
                                     @(NSISOLatin1StringEncoding),
                                     @(NSSymbolStringEncoding),
                                     @(NSNonLossyASCIIStringEncoding),
                                     @(NSShiftJISStringEncoding),
                                     @(NSISOLatin2StringEncoding),
                                     @(NSUnicodeStringEncoding),
                                     @(NSWindowsCP1251StringEncoding),
                                     @(NSWindowsCP1252StringEncoding),
                                     @(NSWindowsCP1253StringEncoding),
                                     @(NSWindowsCP1254StringEncoding),
                                     @(NSWindowsCP1250StringEncoding),
                                     @(NSISO2022JPStringEncoding),
                                     @(NSMacOSRomanStringEncoding),
                                     @(NSUTF16StringEncoding),
                                     @(NSUTF16BigEndianStringEncoding),
                                     @(NSUTF16LittleEndianStringEncoding),
                                     @(NSUTF32StringEncoding),
                                     @(NSUTF32BigEndianStringEncoding),
                                     @(NSUTF32LittleEndianStringEncoding)
                                     ];
            
            NSArray *arrEncodingName = @[@"NSASCIIStringEncoding",
                                         @"NSNEXTSTEPStringEncoding",
                                         @"NSJapaneseEUCStringEncoding",
                                         @"NSUTF8StringEncoding",
                                         @"NSISOLatin1StringEncoding",
                                         @"NSSymbolStringEncoding",
                                         @"NSNonLossyASCIIStringEncoding",
                                         @"NSShiftJISStringEncoding",
                                         @"NSISOLatin2StringEncoding",
                                         @"NSUnicodeStringEncoding",
                                         @"NSWindowsCP1251StringEncoding",
                                         @"NSWindowsCP1252StringEncoding",
                                         @"NSWindowsCP1253StringEncoding",
                                         @"NSWindowsCP1254StringEncoding",
                                         @"NSWindowsCP1250StringEncoding",
                                         @"NSISO2022JPStringEncoding",
                                         @"NSMacOSRomanStringEncoding",
                                         @"NSUTF16StringEncoding",
                                         @"NSUTF16BigEndianStringEncoding",
                                         @"NSUTF16LittleEndianStringEncoding",
                                         @"NSUTF32StringEncoding",
                                         @"NSUTF32BigEndianStringEncoding",
                                         @"NSUTF32LittleEndianStringEncoding"
                                         ];
            
            for (int i = 0 ; i < [arrEncoding count]; i++) {
                unsigned long encodingCode = [arrEncoding[i] unsignedLongValue];
                NSLog(@"arrEncodingName>>(%@)", arrEncodingName[i]);
                NSError *error = nil;
                NSString *filePath = pathString; // <---这里是要查看的文件路径
                NSString *aString = [NSString stringWithContentsOfFile:filePath encoding:encodingCode  error:&error];
                NSLog(@"Error:>>%@", [error localizedDescription]);
                NSData *data = [aString dataUsingEncoding:encodingCode];
                NSString *string = [[NSString alloc] initWithData:data encoding:encodingCode];
                NSLog(@"[string length]>>>%d", [string length]);
                
            }  
        }

你可能感兴趣的:(iOS 判断文件编码格式)