The file “city.txt” couldn’t be opened using text encoding Unicode (UTF-8)

来源:互联网 发布:美国折扣网站 知乎 编辑:程序博客网 时间:2024/05/19 22:51

txt文件  转化为plist文件 

NSString *txtPath = [[NSBundlemainBundle] pathForResource:fileNameofType:@"txt"];

    NSLog(@"_____%@",txtPath);

    NSError *err;

    NSString *txtContent = [NSStringstringWithContentsOfFile:txtPath encoding:NSUTF8StringEncoding error:&err];

    NSLog(@"%@__----____-",err);

The file “city.txt” couldn’t be opened using text encoding Unicode (UTF-8)

这种一般是格式错误引起的,无法读取。我的txt 是中文文件出现乱码,所以用GBK中文编码。需要确认文件编码方式

unsigned long encode =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

    NSData *responseData = [NSDatadataWithContentsOfFile:fileName ];

    NSString *mTxt = [[NSStringalloc] initWithData:responseDataencoding:encode];

NSArray *txtArry = [mTxtcomponentsSeparatedByString:@"\r\n"];


for (NSString *questionin txtArry) {

        NSLog(@"%@_----___",question);

        NSRange range = [questionrangeOfString:@" "];

        if (range.location !=NSNotFound) {

            NSString *answer = [questionsubstringToIndex:range.location];

            NSString *imageName = [questionsubstringFromIndex:range.location+1];

            imageName = [imageName stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];

            

            NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:answer,imageName,nil];

            [plistArry addObject:dic];

             NSLog(@"%lu....%lu,,,,",(unsignedlong)range.location,(unsignedlong)range.length);

            

        }else{

         NSLog(@"%lu....%lu,,,,----",(unsignedlong)range.location,(unsignedlong)range.length);

        }

        

    }

    NSLog(@"%@____%lu",plistArry,(unsignedlong)plistArry.count);

    //写到plist文件中

    NSArray *stringPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *docDirectory = [stringPathobjectAtIndex:0];

    NSString *path = [docDirectorystringByAppendingPathComponent:@"brand.plist"];

    NSLog(@"%@",path);

    [plistArry writeToFile:pathatomically:YES


0 0