cocoa 文件操作
来源:互联网 发布:数据挖掘岗很难进吗 编辑:程序博客网 时间:2024/06/03 16:40
cocoa 文件操作
打开查看文件
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.",nil),
[NSLocalizedString(@"Yes",nil),
[NSLocalizedString(@"No",nil),
nil);
if(n==NSAlertDefaultReturn){
[theManager removeFileAtPath:theFilePath handler:nil];
}
创建文件夹
NSString *theDestination=[[NSHomeDirectory()
stringByAppendingPathComponent:@"Desktop"]
stringByAppendingPathComponent:@"MyNewFolder"];
[theManager createDirectoryAtpath:theDestination
attributes:nil];//第二个参数设置文件夹的属性
打开查看文件
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.",nil),
[NSLocalizedString(@"Yes",nil),
[NSLocalizedString(@"No",nil),
nil);
if(n==NSAlertDefaultReturn){
[theManager removeFileAtPath:theFilePath handler:nil];
}
创建文件夹
NSString *theDestination=[[NSHomeDirectory()
stringByAppendingPathComponent:@"Desktop"]
stringByAppendingPathComponent:@"MyNewFolder"];
[theManager createDirectoryAtpath:theDestination
attributes:nil];//第二个参数设置文件夹的属性
- cocoa 文件操作
- Cocoa文件相关操作
- [Cocoa]_[初级]_[文件拖放操作]
- Cocoa文件管理
- Cocoa 解析xml文件
- Cocoa文件管理
- ios/cocoa 删除文件
- ios/cocoa 删除文件
- [Cocoa] 定制 Prefix.pch文件
- Cocoa
- Cocoa
- Cocoa
- Cocoa
- cocoa-从.nib文件动态生成实例
- Cocoa 修改属性文件(即 .plist 文件)
- Cocoa教学:使Core Data支持恢复操作
- 如何在cocoa application中执行管理员操作
- [Cocoa-4th] Chapter6 - NSTableView 表格操作:增加,删除,编辑
- 在内核中创建文件 filp_open/sys_open
- 2010武汉IT环境~
- 使用NDK进行网络文件传输
- usaco contact
- Prime算法
- cocoa 文件操作
- V4L2采集摄像头数据
- Java集合中常用的接口和类
- SQL/PLUS学习笔记
- java invoke 2【动态调用类的方法】
- Lucene.Net的学习心得
- Nginx源码分析链接
- 带头结点和不带头结点的单链表的尾插法以及各种操作
- RPM 软件包的管理与安装