接上一篇文件管理博文.我们可以用NSPathUtilities.h中包含的NSString函数和分类扩展来以兼容的方式处理路径.下面凡是有系统编程经验的童鞋都知道是啥意思了:
#import <Foundation/Foundation.h> #define msg(...) NSLog(__VA_ARGS__) int main(int argc, char *argv[]){ @autoreleasepool { NSString *short_name = @"love.m"; NSArray *componets; NSString *tmp_dir = NSTemporaryDirectory(); msg(@"tmp_dir : %@",tmp_dir); NSString *full_name = [tmp_dir stringByAppendingPathComponent:short_name]; msg(@"full_name : %@",full_name); NSString *ext_name = [full_name pathExtension]; msg(@"ext_name : %@",ext_name); NSString *home_dir = NSHomeDirectory(); msg(@"home_dir : %@",home_dir); componets = [@"/usr/local/src/linux/xxx.img" pathComponents]; msg(@"every components : %@",componets); NSString *path = [@"~/src/objc_src/love.m" stringByExpandingTildeInPath]; msg(@"expand path : %@",path); path = [path stringByStandardizingPath]; msg(@"expand_way2 path : %@",path); msg(@"user:%@ , full_user:%@",NSUserName(),NSFullUserName()); //last is longer... NSArray *list = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory,\ NSUserDomainMask,YES); msg(@"%@",list); list = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory,\ NSSystemDomainMask|NSLocalDomainMask|NSNetworkDomainMask,YES); msg(@"%@",list); NSString *full_path = [list objectAtIndex:0]; full_path = [full_path stringByAppendingPathComponent:@"love.m"]; msg(@"%@",full_path); } return 0; }
以上代码含义都很清楚就不一一解释了,值得说明的是最后一个函数NSSearchPathForDirectoriesInDomains,如果第二个参数为多个单体枚举的聚集,则结果会是一个数组,代码说的很清楚了,上运行结果:
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./f 2014-07-02 15:00:14.756 f[12608] tmp_dir : /tmp/GNUstepSecure1000 2014-07-02 15:00:14.758 f[12608] full_name : /tmp/GNUstepSecure1000/love.m 2014-07-02 15:00:14.758 f[12608] ext_name : m 2014-07-02 15:00:14.759 f[12608] home_dir : /home/wisy 2014-07-02 15:00:14.759 f[12608] every components : ("/", usr, local, src, linux, "xxx.img") 2014-07-02 15:00:14.759 f[12608] expand path : /home/wisy/src/objc_src/love.m 2014-07-02 15:00:14.759 f[12608] expand_way2 path : /home/wisy/src/objc_src/love.m 2014-07-02 15:00:14.759 f[12608] user:wisy , full_user:wisy,,, 2014-07-02 15:00:14.759 f[12608] ("/home/wisy/GNUstep/Applications") 2014-07-02 15:00:14.759 f[12608] ("/usr/local/lib/GNUstep/Applications", "/usr/lib/GNUstep/Applications") 2014-07-02 15:00:14.759 f[12608] /usr/local/lib/GNUstep/Applications/love.m
接下来好玩的来鸟,何曾几时本猫都对入侵其他进程有着变态的偏执狂欲望(这在本猫的其他博文中略有体现),虽然下面代码只是查询本进程的信息,但不积跬步,何以致千里,我们慢慢来吧:
#import <Foundation/Foundation.h> #define msg(...) NSLog(__VA_ARGS__) int main(int argc, char *argv[]){ @autoreleasepool { NSProcessInfo *proc = [NSProcessInfo processInfo]; NSArray *args = [proc arguments]; //msg(@"all_infos : %@",proc); msg(@"args : %@",args); NSDictionary *env = [proc environment]; msg(@"env : %@",env); msg(@"pid : %d",[proc processIdentifier]); msg(@"process name :%@",[proc processName]); msg(@"hostname : %@",[proc hostName]); msg(@"OS type : %d",(unsigned)[proc operatingSystem]); msg(@"OS name : %@",[proc operatingSystemName]); msg(@"rand string : %@",[proc globallyUniqueString]); msg(@"OS ver : %@",[proc operatingSystemVersionString]); } return 0; }
运行结果如下:
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./f 11 22 33 "aa bb cc" 2014-07-02 15:26:34.711 f[12767] args : ("./f", 11, 22, 33, "aa bb cc") 2014-07-02 15:26:34.713 f[12767] env : {"CLUTTER_IM_MODULE" = xim; COLORTERM = "gnome-terminal"; "DBUS_SESSION_BUS_ADDRESS" = "unix:abstract=/tmp/dbus-obtBgG4wMe"; "DEFAULTS_PATH" = "/usr/share/gconf/xfce.default.path"; "DESKTOP_SESSION" = xfce; DISPLAY = ":0.0"; GDMSESSION = xfce; "GDM_LANG" = "zh_CN"; "GLADE_CATALOG_PATH" = ":"; "GLADE_MODULE_PATH" = ":"; "GLADE_PIXMAP_PATH" = ":"; "GNOME_KEYRING_CONTROL" = "/run/user/1000/keyring-1BJWxI"; "GNOME_KEYRING_PID" = 1714; "GTK_IM_MODULE" = fcitx; "GTK_MODULES" = "overlay-scrollbar:unity-gtk-module"; HOME = "/home/wisy"; "IM_CONFIG_PHASE" = 1; INSTANCE = ""; JOB = dbus; LANG = "zh_CN.UTF-8"; LANGUAGE = "zh_CN:zh"; LESSCLOSE = "/usr/bin/lesspipe %s %s"; LESSOPEN = "| /usr/bin/lesspipe %s"; LOGNAME = wisy; "LS_COLORS" = "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;300;36:/"; "UBUNTU_MENUPROXY" = 1; "UPSTART_EVENTS" = "started xsession"; "UPSTART_INSTANCE" = ""; "UPSTART_JOB" = startxfce4; "UPSTART_SESSION" = "unix:abstract=/com/ubuntu/upstart-session/1000/1717"; USER = wisy; "VTE_VERSION" = 3409; WINDOWID = 62914568; XAUTHORITY = "/home/wisy/.Xauthority"; "XDG_CONFIG_DIRS" = "/etc/xdg/xdg-xfce:/usr/share/upstart/xdg:/etc/xdg:/etc/xdg"; "XDG_CURRENT_DESKTOP" = XFCE; "XDG_DATA_DIRS" = "/usr/share/xfce:/usr/share/xfce4:/usr/local/share/:/usr/share/:/usr/share"; "XDG_GREETER_DATA_DIR" = "/var/lib/lightdm-data/wisy"; "XDG_MENU_PREFIX" = "xfce-"; "XDG_RUNTIME_DIR" = "/run/user/1000"; "XDG_SEAT" = seat0; "XDG_SEAT_PATH" = "/org/freedesktop/DisplayManager/Seat0"; "XDG_SESSION_ID" = c2; "XDG_SESSION_PATH" = "/org/freedesktop/DisplayManager/Session0"; "XDG_VTNR" = 7; XMODIFIERS = "@im=fcitx"; "_" = "./f";(太长截取一部分!) } 2014-07-02 15:26:34.714 f[12767] pid : 12767 2014-07-02 15:26:34.714 f[12767] process name :f 2014-07-02 15:26:34.715 f[12767] hostname : wisy-ThinkPad-X61 2014-07-02 15:26:34.715 f[12767] OS type : 100 2014-07-02 15:26:34.715 f[12767] OS name : GSGNULinuxOperatingSystem 2014-07-02 15:26:34.715 f[12767] rand string : wisy-ThinkPad-X61_31df_1963ebaa_0 2014-07-02 15:26:34.715 f[12767] OS ver : 3.13.0-24-generic
第三种文件拷贝方式,我们使用了NSFileHandle类,该类功能的一个简单流程就是:
1 打开文件
2 对打开得文件执行I/O操作
3 关闭文件
#import <Foundation/Foundation.h> #define msg(...) NSLog(__VA_ARGS__) int main(int argc,char *argv[]) { @autoreleasepool{ NSFileHandle *in_file,*out_file; NSData *buf; in_file = [NSFileHandle fileHandleForReadingAtPath:@"x.db"]; if(in_file == nil){ msg(@"open in_file failed"); return 1; } [[NSFileManager defaultManager] createFileAtPath:@"out.db" \ contents:nil attributes:nil]; out_file = [NSFileHandle fileHandleForWritingAtPath:@"out.db"]; if(out_file == nil){ msg(@"open out_file failed"); return 2; } //可能包含数据,先截断。 [out_file truncateFileAtOffset:0]; buf = [in_file readDataToEndOfFile]; [out_file writeData:buf]; [in_file closeFile]; [out_file closeFile]; msg(@"%@",[NSString stringWithContentsOfFile:@"out.db" \ encoding:NSUTF8StringEncoding error:NULL]); } return 0; }
NSURL类可以从网页上读取对象,这个前面有一个例子,这里不再展开说了,自己度娘哦。
最后还有一个NSBundle类,我简单看了下,类似于windows中的对程序中资源文件的定位操作,不过os x和ios资源的自包含特性更强一些;在ios中进程都在沙盒中运行读取的/目录统统都是虚拟出来的,否则全乱套鸟。貌似扯多鸟...