//---------------------------------------------------------------------------------
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. HUD:4405-Aeroplane chess(期望飞行棋)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...

  2. selenium2 TestNG参数化

    想要参数化,首先要加入@Parameters({"参数1","参数2"}) package com.demo.test; import java.util.co ...

  3. 2049: [Sdoi2008]Cave 洞穴勘测(LCT)

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 9962  Solved: 4824[Submit] ...

  4. hdu 1011 Starship Troopers(树形背包)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. static_cast 、const_cast、dynamic_cast、reinterpret_cast 关键字简单解释

    static_cast .const_cast.dynamic_cast.reinterpret_cast 关键字简单解释: Static_cast 静态类型转换 ①用于类层次结构中基类(父类)和派生 ...

  6. hexo博客发布注意事项

    最近把hexo博客内容写完了,就发布到github上面去,结果就出现各种一些小问题. 1.发布之后,hexo博客的css与js无法访问. 原因:没有配置正确的url路径.(配置文件_config.ym ...

  7. 设计模式(八)组合模式 Composite

    组合模式: 允许你将对象组合成树形结构来表现“整体/部分”层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 组合模式适用于创建复杂的对象,这个对象包含某些个别的对象以及这些对象的组合. 从 ...

  8. HDU2098 分拆素数和

    Problem Description 把一个偶数拆成两个不同素数的和,有几种拆法呢?   Input 输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束.   Outp ...

  9. 【bzoj3251】树上三角形 朴素LCA+暴力

    题目描述 给定一大小为n的有点权树,每次询问一对点(u,v),问是否能在u到v的简单路径上取三个点权,以这三个权值为边长构成一个三角形.同时还支持单点修改. 输入 第一行两个整数n.q表示树的点数和操 ...

  10. 刷题总结——array(ssoj)

    题目: 题目描述 给定 2 个正整数序列 A1, A2,序列长度分别为 L1, L2.你可以进行以下的一次操作:1. 选择两个数 K1,K2(1≤K1≤L1, 1≤K2≤L2):2. 移去 A1 中最 ...