ios读取文件的内容|ios如何使用 Xcode

|

㈠ 如何读取IOS共享目录的文件

NSString *home = NSHomeDirectory();//获取沙盒路径//拼接Documents路径//NSString *docPath = [home stringByAppendingStringt:@"/Documents"];NSString *docPath = [home :@"Documents"];NSString *filePath = [docPath :@"data.plist"];NSArray *array = @[@1,@2,@"123"];//只有具备writeToFile的对象才能使用plist存储,NSArray[array writeToFile:filePath atomically:YES];主要是获取路径,然后把文件写到这个路径里就好了

㈡ IOS开发,已经解析出JSON,如何获取里面的内容

1,首先获取文件目录 *paths = (NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];chatFile = [[NSString alloc] initWithString:[documentsDirectory :CHATSFILE_NAME]];2,加载文件- (void) loadThread:(NSString *)xmlFile {NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];NSXMLParser *chatLogParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURLfileURLWithPath:xmlFile]];[chatLogParser setDelegate:self];[currentString setString:@""];//记录当前节点的值[currentChatInfo removeAllObjects];//节点对象[chatLogParser parse];//开始XML解析chatLogParser release];[self performSelectorOnMainThread:@selector(finshLoadFile) withObject:nil waitUntilDone:YES];//创建线程[pool release];}

㈢ ios关于文件的读取

ios关于文件的读取有四种方法:#import <foundation foundation.h="">int main(int argc, const char * argv[]){ @autoreleasepool { //第一种方法: NSFileManager实例方法读取数据 NSArray* paths = (NSDesktopDirectory, NSUserDomainMask, YES); NSString* thepath = [paths lastObject]; thepath = [thepath :@"fd_list.txt"]; NSLog(@"桌面目录:%@", thepath); NSFileManager* fm = [NSFileManager defaultManager]; NSData* data = [[NSData alloc] init]; data = [fm contentsAtPath:thepath]; NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); //第二种方法: NSData类方法读取数据 data = [NSData dataWithContentsOfFile:thepath]; NSLog(@"NSData类方法读取的内容是:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); //第三种方法: NSString类方法读取内容 NSString* content = [NSString stringWithContentsOfFile:thepath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"NSString类方法读取的内容是:\n%@",content); //第四种方法: NSFileHandle实例方法读取内容 NSFileHandle* fh = [NSFileHandle fileHandleForReadingAtPath:thepath]; data = [fh readDataToEndOfFile]; NSLog(@"NSFileHandle实例读取的内容是:\n%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); } return 0;}</foundation>

㈣ ios开发怎么读取plist文件

首先要知道读取plist文件的方法,一般来说,使用代码NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"listFileName" ofType:@"plist"];NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];已经足够了,此时可以使用NSLog例程查看array和dictionary的内容。不过,有时候受plist文件内容的限制,array内容可能为空。其实,用dictionary就已经足够了,在下面的例子里我们也只用dictionary。1、运行Xcode4.2,新建一个Single View Application,名称为ReadPlistFile,其他设置如下图:2、新建我们自己的plist文件:File —> New —> New File,选择Mac OS X下的Property List文件名为 customInfo,Group选择Supporting Files。3、单击新建的customInfo.plist,我们添加数据,如下图:注意,Type一项的类型,选择的是Dictionary,以Source Code打开,显示如下:<?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><dict><key>Student</key><dict><key>Name</key><string>Yang</string><key>Sex</key><string>Male</string><key>Num</key><string>SX_010</string></dict><key>Mentor</key><dict><key>Name</key><string>Gu</string><key>Sex</key><string>Male</string></dict></dict></plist>4、为视图添加控件:单击BIDViewController.xib,打开IB,拖几个控件上去,并设置好布局,如下图:上图中所有的控件都是Label,并设置了字体大小。5、接下来就是映射呗,把五个灰色的Label都映射到BIDViewController.h文件中,类型都是OutLet,名称依次是stuName,stuSex,stuNum,mtName,mtSex。6、单击BIDViewController.m,在viewDidLoad方法中的[super viewDidLoad]之后添加如下代码://首先读取studentInfo.plist中的数据NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"customInfo" ofType:@"plist"];NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];//将学生信息填入视图NSDictionary *tmpInfo = [dictionary objectForKey: @"Student"];self.stuName.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Name"]];self.stuSex.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Sex"]];self.stuNum.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Num"]];//将导师信息写入视图tmpInfo = [dictionary objectForKey: @"Mentor"];self.mtName.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Name"]];self.mtSex.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Sex"]];7、运行,查看效果:

㈤ ios如何使用 Xcode,读取和写入存在的数据库文件

通过将 sqlite db 文件添加到您的支持文件组在 Xcode 中,只添加的文件到应用程序的包以便在生成过程中它获取与所有其他资源打包。因为应用程序不能写入到其捆绑,你必须 sqlite 数据库从复制文件捆绑到一个可写的位置例如#define FORCE_RECOPY_DB NO- (void)DatabaseIfNeeded { NSFileManager *fm = [[NSFileManager alloc] init]; NSString *documentsPath = [(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *destinationPath = [documentsPath :@"pte.sqlite"]; void (^Db)(void) = ^(void){ NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"pte" ofType:@"sqlite"]; NSAssert1(sourcePath, @"source db does not exist at path %@",sourcePath); NSError *Error = nil; if( ![fm ItemAtPath:sourcePath toPath:destinationPath error:&Error] ) { DDLogError(@"ERROR | db could not be copied: %@", Error); } }; if( FORCE_RECOPY_DB && [fm fileExistsAtPath:destinationPath] ) { [fm removeItemAtPath:destinationPath error:NULL]; Db(); } else if( ![fm fileExistsAtPath:destinationPath] ) { DDLogInfo(@"INFO | db file needs ing"); Db(); }}现在当您想要打开的数据库,使用中的文件路径的位置。请注意你就不能来检查您的项目中的 sqlite db 文件和期望找到写入从您的代码的更改。(因为您的代码将现在工作与复制的 sqlite 文件。)

㈥ iOS怎么遍历目录读取文件进来回复都有分

首先,你只能访问你的app沙盒中的目录.要遍历的话就需要你自己写递归算法. [[NSFileManager defaultManager] contentsOfDirectoryAtPath:<#(NSString *)#> error:<#(NSError **)#>] 这个方法可以获取指定路径下面的内容.你可以点击工程的procts 里面编译出来的.app文件,右键点击 然后选择show in finder.你就可以找到app的位置了.然后你再显示包内容就可以看到沙盒里面的东西了.

㈦ 苹果手机如何读取u盘

苹果抄手机手机借助外部是袭可以读取U盘的。需要在APP Store下载i-usb-storer。

1、自动跳入APP Store下载所需要的APP;

㈧ iphone手机上如何查看储存里面的文件

iphone手机上查看储存里面的文件方法:

1、首先我们打开苹果手机,在屏幕上找内到设置按钮,点击容进入设置界面。

㈨ ios中怎么读取resources路径下的文件

NSArray *array = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:@"image"];NSLog(@"array:%@",array);首先需要在mac上新建一个文件夹叫image, 放一些图片进去,然后把整个文件夹拖到项目中,在弹出的对话框中选择:Create folder references for any added folders ,这个会创建一个真实的目录,否则找到不这个目录下的文件家目录下共有四个子目录:Documents 目录:您应该将所有的应用程序数据文件写入到这个目录下。这个目录用于存储用户数据或其它应该定期备份的信息。AppName.app 目录:这是应用程序的程序包目录,包含应用程序的本身。由于应用程序必须经过签名,所以您在运行时不能对这个目录中的内容进行修改,否则可能会使应用程序无法启动。Library 目录:这个目录下有两个子目录:Caches 和 PreferencesPreferences 目录包含应用程序的偏好设置文件。您不应该直接创建偏好设置文件,而是应该使用NSUserDefaults类来取得和设置应用程序的偏好Caches 目录用于存放应用程序专用的支持文件,保存应用程序再次启动过程中需要的信息。tmp 目录:这个目录用于存放临时文件,保存应用程序再次启动过程中不需要的信息。获取这些目录路径的方法:1,获取家目录路径的函数:NSString *homeDir = NSHomeDirectory();2,获取Documents目录路径的方法:NSArray *paths = (NSDocumentDirectory, NSUserDomainMask, YES);NSString *docDir = [paths objectAtIndex:0];3,获取Caches目录路径的方法:NSArray*paths=(NSCachesDirectory, NSUserDomainMask, YES);NSString *cachesDir = [paths objectAtIndex:0];4,获取tmp目录路径的方法:NSString *tmpDir = NSTemporaryDirectory();5,获取应用程序程序包中资源文件路径的方法:例如获取程序包中一个图片资源(apple.png)路径的方法:NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];代码中的mainBundle类方法用于返回一个代表应用程序包的对象。文件IO1,将数据写到Documents目录:- (BOOL)writeApplicationData:(NSData *)data toFile:(NSString *)fileName {NSArray *paths = (NSDocumentDirectory, NSUserDomainMask, YES);NSString *docDir = [paths objectAtIndex:0]; if (!docDir) { NSLog(@”Documents directory not found!”); return NO; }NSString *filePath = [docDir :fileName];return [data writeToFile:filePath atomically:YES];}2,从Documents目录读取数据:- (NSData *)applicationDataFromFile:(NSString *)fileName {NSArray *paths = (NSDocumentDirectory, NSUserDomainMask, YES);NSString *docDir = [paths objectAtIndex:0];NSString *filePath = [docDir :fileName];NSData *data = [[[NSData alloc] initWithContentsOfFile:filePath] autorelease]; return data;}这个主要就是返回一个绝对路径用来存放我们需要储存的文件。- (NSString *)dataFilePath {NSArray *paths = (NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];return [documentsDirectory :@"shoppingCar.plist"];}NSFileManager* fm=[NSFileManager defaultManager];if(![fm fileExistsAtPath:[self dataFilePath]]){//下面是对该文件进行制定路径的保存[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];//取得一个目录下得所有文件名NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];//读取某个文件NSData *data = [fm contentsAtPath:[self dataFilePath]];//或者NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];}

㈩ ios 如何读取word内容

在iOS设备(iPhone,iPad)上编辑Word文档等Office办公软件有Apple公司的iWork(收费),Google公司的GoogleDocs(在线版),QuickOffice(收费),金山公版司的WPSForiOS(免费,尽权支持演示文档),DataViz公司的DocumentsToGo(收费)软件,OliveOfficePremium,PolarisOffice,ThinkfreeOffice,SmartOffice等。这些办公软件能编辑Word,Excel,PowerPoint等文件,阅读PDF文件。在AppleStore上搜索安装即可。


赞 (0)