0
我想在我的应用程序文档文件夹中创建一个文件,并且我必须存储一些内容。该内容来自我的本地主机文件。所以我写了一个下面的代码。但是这段代码并未在我的应用程序文档文件夹中创建一个文件。创建文件到应用程序文档文件夹
- (void) createFile
{
NSString *strPath = [NSString stringWithFormat:@"http://192.168.5.117/~mac/banana.obj"];
NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:strPath]];
NSString *strLastpath = [strPath lastPathComponent];
NSString *folderName = [@"Object File/" stringByAppendingString:strLastpath];
NSLog(@"Path : %@ \n File Name : %@",strPath,folderName);
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:folderName]];
NSLog(@"Database Path : %@",databasePath);
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath:databasePath] == YES)
{
NSLog(@"File Exists");
}
else
{
NSLog(@"File not exists");
}
NSString *content = [NSString stringWithFormat:@"%@",data];
[content writeToFile:strLastpath atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
}
虽然build for running我得到了这个。
2013-10-11 11:36:38.833 SampleFileCreation[1321:c07] Path : http://192.168.5.117/~mac/banana.obj
File Name : Object File/banana.obj
2013-10-11 11:36:38.834 SampleFileCreation[1321:c07] Database Path : /Users/mac/Library/Application Support/iPhone Simulator/6.1/Applications/4FA749DF-D12D-4956-AF76-140D2F981F17/Documents/Object File/banana.obj
2013-10-11 11:36:38.834 SampleFileCreation[1321:c07] File not exists
确定的NSData *数据= [NSData的dataWithContentsOfURL:[NSURL URLWithString:strPath的];这是给你下载的数据? – Tirth
是的,我得到了所有的内容,我使用NSLog检查它。 – Sabs
你要下载哪种文件类型? – Tirth