
打开查看文件
NSOpenPanel *openPanel=[NSOpenPanel openPanel];
[openPanel setTitle:@"Choose a file or Folder"];//setTitle为NSWindow的方法,它是openPanel 的父类
[openPanel setCanChooseDirectorIEs:YES];//默认不可以选文件夹,可选任何格式文件
NSInteger i=[openPanel runModal];//显示openPanel
if(i==NSOKbutton){
Nsstring *thefilePath=[openPanel filename];
//给outlets(filePathdisplay)赋值:[filePathdisplay setStringValue:thefilePath]
NSfileManager *theManager=[NSfileManage defaultManager];
Nsstring *thefilename=[theManage displayNameatPath:thefilePath];
if([theManager fileExistsAtPath:thefilePath]){
//文件存在
}
if( [theManager fileExistsAtPath:thefilePath isDirectory:&isFolder] ){
//表示选中的是一个目录(文件夹)
NSDictionary *thefileAttributes=[theManager fileAttributesAtPath:thefilePath traverselink:YES];
//NSDictionary是一个数据结构,其中包括:文件大小,创建日期,修改日期
//由于只是读数据,则不用可变的NSMutableDictionary
NSNumber *thefileSize=[thefileAttributes objectForKey:NSfileSize];
NSDate *theModificationDate=[thefileAttributes objectForKey:NSfileModificationDate];
NSDate *theCreationDate=[thefileAttributes objectForKey:NSfileCreationDate];
//查看文件图标(要先用NSfileWrapper把文件数据放入内存)
NSfileWrapper *thefileWrapper=[[NSfileWrapper alloc] initWithPath:thefilePath] autorelease];
NSImage *theIcon=[thefileWrapper icon];
//fileIcondisplay为Interface上的NSImageVIEw对象(library中的Image well)
[fileIcondisplay setimageScaling:NSScaletoFit];
[fileIcondisplay setimage:theIcon];
}
可以实现对对文档(Text),图片,以及多媒体文件的 *** 作
复制文件
Nsstring *theDestination=[[NSHomeDirectory() //NSHomeDirectory是Foundation的方法
stringByAppendingPathComponent:@"Desktop"] //文件保存的目录
stringByAppendingPathComponent:thefilename];
[theManager copyPath:thefilePath topath:theDestination handler:nil];
移动(剪切)文件
[theManager movePath:thefilePath topath:theDestination handler:nil];
删除文件
NSInteger n=NSRunAlertPanel(
[NSLocalizedString(@"Are you sure you want to delete the file?",nil),
[NSLocalizedString(@"You cannot undo this deletion.",51); Font-family:Arial; Font-size:14px; line-height:26px"> [NSLocalizedString(@"Yes",51); Font-family:Arial; Font-size:14px; line-height:26px"> [NSLocalizedString(@"No",51); Font-family:Arial; Font-size:14px; line-height:26px"> nil);
if(n==NSAlertDefaultReturn){
[theManager removefileAtPath:thefilePath handler:nil];
}
创建文件夹
Nsstring *theDestination=[[NSHomeDirectory()
stringByAppendingPathComponent:@"Desktop"]
stringByAppendingPathComponent:@"MyNewFolder"];
[theManager createDirectoryAtpath:theDestination
attributes:nil]; //第二个参数设置文件夹的属性
总结以上是内存溢出为你收集整理的Cocoa文件相关 *** 作全部内容,希望文章能够帮你解决Cocoa文件相关 *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)