A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wangshengjie 中级黑马   /  2015-3-14 19:42  /  988 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

在ObjC中路径、文件读写等操作是利用字符串来完成的,这里通过几个简单的例子来演示(首先在桌面上新建一个test.txt文件,里面存储的内容是”hello world,世界你好!”)
  1. //  main.m
  2. //  FoundationFramework
  3. //
  4. //  Created by Kenshin Cui on 14-2-16.
  5. //  Copyright (c) 2014年 Kenshin Cui. All rights reserved.
  6. //

  7. #import <Foundation/Foundation.h>


  8. void test1(){
  9.     //读取文件内容
  10.     NSString *path=@"/Users/kenshincui/Desktop/test.txt";
  11.     NSString *str1=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  12.     //注意上面也可以使用gb2312 gbk等,例如kCFStringEncodingGB_18030_2000,但是需要用CFStringConvertEncodingToNSStringEncoding转换
  13.     NSLog(@"str1 is %@",str1);
  14.     //结果:str1 is hello world,世界你好!

  15.    
  16.    
  17.    
  18.     //上面我们看到了读取文件,但并没有处理错误,当然在ObjC中可以@try @catch @finnally但通常我们并不那么做
  19.     //由于我们的test.txt中有中文,所以使用下面的编码读取会报错,下面的代码演示了错误获取的过程
  20.     NSError *error;
  21.     NSString *str2=[NSString stringWithContentsOfFile:path encoding:kCFStringEncodingGB_18030_2000 error:&error];//注意这句话中的error变量是**error,就是指针的指针那就是指针的地址,由于error就是一个指针此处也就是error的地址&error,具体原因见下面补充
  22.     if(error){
  23.         NSLog(@"read error ,the error is %@",error);
  24.     }else{
  25.         NSLog(@"read success,the file content is %@",str2);
  26.     }
  27.     //结果:read error ,the error is Error Domain=NSCocoaErrorDomain Code=261 "The file couldn’t be opened using the specified text encoding." UserInfo=0x100109620 {NSFilePath=/Users/kenshincui/Desktop/test.txt, NSStringEncoding=1586}

  28.    
  29.    
  30.    
  31.     //读取文件内容还有一种方式就是利用URl,它除了可以读取本地文件还可以读取网络文件
  32.     //NSURL *url=[NSURL URLWithString:@"file:///Users/kenshincui/Desktop/test.txt"];
  33.     NSURL *url=[NSURL URLWithString:@"http://www.apple.com"];
  34.     NSString *str3=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
  35.     NSLog(@"str3 is %@",str3);
  36. }
  37. void test2(){
  38.     //下面是文件写入
  39.     NSString *path1=@"/Users/kenshincui/Desktop/test2.txt";
  40.     NSError *error1;
  41.     NSString *str11=@"hello world,世界你好!";
  42.     [str11 writeToFile:path1 atomically:YES encoding:NSUTF8StringEncoding error:&error1];//automically代表一次性写入,如果写到中间出错了最后就全部不写入
  43.     if(error1){
  44.         NSLog(@"write fail,the error is %@",[error1 localizedDescription]);//调用localizedDescription是只打印关键错误信息
  45.     }else{
  46.         NSLog(@"write success!");
  47.     }
  48.     //结果:write success!
  49. }
  50. //路径操作
  51. void test3(){
  52.     NSMutableArray *marray=[NSMutableArray array];//可变数组
  53.     [marray addObject:@"Users"];
  54.     [marray addObject:@"KenshinCui"];
  55.     [marray addObject:@"Desktop"];

  56.     NSString *path=[NSString pathWithComponents:marray];
  57.     NSLog(@"%@",path);//字符串拼接成路径
  58.     //结果:Users/KenshinCui/Desktop

  59.     NSLog(@"%@",[path pathComponents]);//路径分割成数组
  60.     /*结果:
  61.      (
  62.         Users,
  63.         KenshinCui,
  64.         Desktop
  65.     )
  66.     */

  67.     NSLog(@"%i",[path isAbsolutePath]);//是否绝对路径(其实就是看字符串是否以“/”开头)
  68.     //结果:0
  69.     NSLog(@"%@",[path lastPathComponent]);//取得最后一个目录
  70.     //结果:Desktop
  71.     NSLog(@"%@",[path stringByDeletingLastPathComponent]);//删除最后一个目录,注意path本身是常量不会被修改,只是返回一个新字符串
  72.     //结果:Users/KenshinCui
  73.     NSLog(@"%@",[path stringByAppendingPathComponent:@"Documents"]);//路径拼接
  74.     //结果:Users/KenshinCui/Desktop/Documents
  75. }
  76. //扩展名操作
  77. void test4(){
  78.     NSString *path=@"Users/KenshinCui/Desktop/test.txt";
  79.     NSLog(@"%@",[path pathExtension]);//取得扩展名,注意ObjC中扩展名不包括"."
  80.     //结果:txt
  81.     NSLog(@"%@",[path stringByDeletingPathExtension]);//删除扩展名,注意包含"."
  82.     //结果:Users/KenshinCui/Desktop/test
  83.     NSLog(@"%@",[@"Users/KenshinCui/Desktop/test" stringByAppendingPathExtension:@"mp3"]);//添加扩展名
  84.     //结果:Users/KenshinCui/Desktop/test.mp3
  85. }

  86. int main(int argc, const char * argv[]) {
  87.     test1();
  88.     test2();
  89.     test3();
  90.     test4();
  91.     return 0;
  92. }
复制代码
注意:在上面的例子中我们用到了可变数组,下面会专门介绍。

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马