How To Read a File From Your Application Bundle
来源:互联网 发布:yaosir乒乓器材淘宝店 编辑:程序博客网 时间:2024/05/22 12:32
First you need to add your file to the Resources folder of your Xcode project. Then you can access the file like this (assuming the file is called MyFile.txt):
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];
- NSData *myData = [NSData dataWithContentsOfFile:filePath];
- if (myData) {
- // do something useful
- }
Here’s a complete example reading a help text file into a UIWebView.
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];
- NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
- if (htmlData) {
- [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];
- }
If you want to read the file into a string, which you can then display in a UITextView, for example, then do this:
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];
- if (filePath) {
- NSString *myText = [NSString stringWithContentsOfFile:filePath];
- if (myText) {
- textView.text= myText;
- }
- }
- How To Read a File From Your Application Bundle
- How to read data from a file in reverse order?
- How to read a PCap file from Wireshark with C++
- How to read a frame from YUV file in OpenCV?
- How to trigger your application while downloading file from wap browser(symbian)?
- How to open/read/write a local file from an applet
- How to drag a virtual file from your app into Windows Explorer
- How to read data from csv file in c#
- Windows Batch File: How to read a file
- How to manually remove an infected file from your computer
- Saving data to a file in your Android application
- Tcl/TK -- How to read a ini file
- How to read a file line by line?
- Shell: How to read lines in a file.
- How to remove a line from a file in java?
- How to Remove PDF/A Information from a file
- How to create an EMF model from a Java application ?
- Read a file from xml
- php+mysql+nigix
- asp.net获取客户端IP地址方法
- VC 获取操作系统语言信息 Language Sublanguage
- 整型数转换为指定位数的字符串,位数不足前面补零
- SSH架构简单总结
- How To Read a File From Your Application Bundle
- 性能监控工具
- 学习算法导论-插入查询
- db2整理
- [OOAD]设计模式之适配器(Adapter)模式
- 关于多元线性回归
- 对于JavaScript的 Stack overflow at line 错误总结
- SQL常用函数之综合
- Domino安装配置系列之一:系统环境要求配置表