//---------------------------------------------------------------------------------
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *homeDir= NSHomeDirectory();//获取AppHome目录
NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];//获取document目录然后将文件名追加进去
NSString *tmpPath = NSTemporaryDirectory(); //获取tmp目录
NSLog(@"%@",tmpPath);
//在Documents下面创建一个test目录--------------------------------------------------------------
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
BOOL ok = [fileManager createDirectoryAtPath:myDirectory attributes:nil];
NSLog(@"%@",myDirectory);
//取得一个目录下的所有文件名-------------------------------------------------------------------
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSError *error = nil;
NSArray *fileList = [[NSArray alloc] init];
//fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];
NSLog(@"%@",fileList);
//创建文件--------------------------------------------------------------------------------------
//NSString *myDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test"];
//更改到待操作的目录下
[fileManager changeCurrentDirectoryPath:[tmpPath stringByExpandingTildeInPath]];//切换当前目录为document目录
//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
[fileManager createFileAtPath:@"fileName"contents:nil attributes:nil];
//---------------------------------------------------------------------------------
[pool drain];
return 0;
}

bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.

我们的程序是一个bundle. 在Finder中,一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的main bundle

bundle中的有些资源可以本地化.例如,对于foo.nib,我们可以有两个版本: 一个针对英语用户,一个针对法语用户. 在bundle中就会有两个子目录:English.lproj和French.lproj,我们把各自版本的foo.nib文件放到其中. 当程序需要加载foo.nib文件时,bundle会自动根据所设置的语言来加载. 我们会在16章再详细讨论本地化

通过使用下面的方法得到程序的main bundle
NSBundle *myBundle = [NSBundle mainBundle];

一般我们通过这种方法来得到bundle.如果你需要其他目录的资源,可以指定路径来取得bundle
NSBundle *goodBundle;
goodBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];

一旦我们有了NSBundle 对象,那么就可以访问其中的资源了
// Extension is optional
NSString *path = [goodBundle pathForImageResource:@"Mom"];
NSImage *momPhoto = [[NSImage alloc] initWithContentsOfFile:path];

bundle中可以包含一个库. 如果我们从库得到一个class, bundle会连接库,并查找该类:
Class newClass = [goodBundle classNamed:@"Rover"];
id newInstance = [[newClass alloc] init];

如果不知到class名,也可以通过查找主要类来取得
Class aClass = [goodBundle principalClass];
id anInstance = [[aClass alloc] init];

可以看到, NSBundle有很多的用途.在这当中, NSBundle负责(在后台)加载nib文件. 我们也可以不通过NSWindowController来加载nib文件, 直接使用NSBundle:
BOOL successful = [NSBundle loadNibNamed:@"About" owner:someObject];
注意噢, 我们指定了一个对象someObject作为nib的File's Owner

使用initWithContentsOfFile时,文件路径的写法使用initWithContentsOfFile方法可以通过读取一个文件的内容来初始化对象。 但文件的路径应该怎么确定呢?可以使用NSBundle的对象来获取。例如当前程序所在目录下有个文件re.xml,我们要将该文件的内容做为NSData的数据源来初始化一个NSData对象,可以用下面的方法来实现:
 
NSString *filePath = [[NSBundle mainBundle] pathForResouse:@"re" ofType:@"xml"]; NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
 
读取plist中的内容:
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; self.data = [NSArray arrayWithContentsOfFile:dataPath];
 
删除本地文件
NSString * thePath=[self getUserDocumentDirectoryPath];
NSMutableString * fullPath=[[[NSMutableString alloc]init]autorelease];
[fullPath appendString:thePath];
NSString * idString=[idArray objectAtIndex:indexPath.row];
NSString * coverName=[NSString stringWithFormat:@"/%@.jpg",idString];
[fullPath appendString:coverName];
NSFileManager *defaultManager;
defaultManager = [NSFileManager defaultManager];
 
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error BOOL boolValue=[defaultManager removeItemAtPath: fullPath error: nil];
 
if (boolValue) {
NSLog(@"remove cover image ok");
}
 
- (NSString*)getUserDocumentDirectoryPath {
NSArray* array = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask,YES);
 
if([array count])
return [array objectAtIndex: 0];
else return @"";
 
}

获取path的几种方式:NSFileManager NSHomeDirectory NSBundle的更多相关文章

  1. java:struts框架2(方法的动态和静态调用,获取Servlet API三种方式(推荐IOC(控制反转)),拦截器,静态代理和动态代理(Spring AOP))

    1.方法的静态和动态调用: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...

  2. 获取Type的三种方式

    using System;using UnityEngine; public class Type_Test : MonoBehaviour{    private void Awake()    { ...

  3. java动态获取WebService的两种方式(复杂参数类型)

    java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_Fligh ...

  4. AngularJS中获取数据源的几种方式

    在AngularJS中,可以从$rootScope中获取数据源,也可以把获取数据的逻辑封装在service中,然后注入到app.run函数中,或者注入到controller中.本篇就来整理获取数据的几 ...

  5. java 获取时间戳的三种方式

      java 获取时间戳的三种方式 CreationTime--2018年7月13日16点29分 Author:Marydon 1.实现方式 方式一:推荐使用 System.currentTimeMi ...

  6. 【Struts2】Struts2获取session的三种方式

    1.Map<String,Object> map =  ActionContext.getContext().getSession(); 2.HttpSession session = S ...

  7. js获取时间戳的三种方式

      js获取时间戳的三种方式 CreateTime--2018年5月23日08:44:10 Author:Marydon // 方式一:推荐使用 var timestamp=new Date().ge ...

  8. Struts2(四.注册时检查用户名是否存在及Action获取数据的三种方式)

    一.功能 1.用户注册页面 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  9. HTTP获取信息的四种方式

    HTTP 从网络获取信息的四种方式 GET GET指代你在浏览器中输入网址,浏览网站时做的事.例如,我们使用 http://www.baidu.com 的时候,可以将GET想象成他说:"hi ...

随机推荐

  1. 同一条sql在mysql5.6和5.7版本遇到的问题。

    之前用的是mysql 5.6版本,执行select * from table group by colunm 是可以出结果的, 但是切换的5.7版本,这条sql就报错, Expression #1 o ...

  2. 2019 study list

    分析工具: (1)SQL   select from   where   group by having   order by   limit   运算符(算数运算符+-*/.比较运算符>< ...

  3. python基础学习笔记——类的成员

    一. 细分类的组成成员 之前咱们讲过类大致分两块区域,如下图所示: 每个区域详细划分又可以分为: class A: company_name = '老男孩教育' # 静态变量(静态字段) __ipho ...

  4. loj2024「JLOI / SHOI2016」侦查守卫

    too hard #include <iostream> #include <cstdio> using namespace std; int n, d, m, uu, vv, ...

  5. 理解Linux虚拟文件系统VFS

    当前,除了linux标准的文件系统Ext2/Ext3/Ext4外,还有很多种文件系统,比如reiserfs, xfs, Windows的vfat NTFS,网络文件系统nfs 以及flash 文件系统 ...

  6. CM10 WIFI连不上解决方案

    手机是Moto Mileston2 ,好久之前就刷成了CM10, 但是一直没出问题. 最近,发现在某些路由器上连接不上,总是 在验证账户或者获取IP. 解决办法如下: http://moto.zol. ...

  7. ACM-ICPC 2018 沈阳赛区网络预赛 J树分块

    J. Ka Chang Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero p ...

  8. C语言总结(5)

    一.函数的定义: 函数是一个完成特定工作的独立程序模块,包括库函数和自定义函数.例如:scanf(),printf()为库函数,cylinder(),fact()为自定义函数. 函数定义的一般形式: ...

  9. Xshell设置登录会话

    新建会话 点击用户登录验证输入账号密码 如果是公钥登录,选择pubulic key登录

  10. VMware RHEL6.3 开启网络连接

    确认/etc/sysconfig/network是否存在,如果不存在,service network 命令使用不了.新建: NETWORKING=yes HOSTNAME=RHEL6. GATEWAY ...