2012-06-07 115 views
5

我在名为newFolder.So的文档目录中创建一个文件夹现在我想知道如何在我的文件夹中创建txt文件,并使用newFolder创建我现在如何在文档目录中创建文件但如何在我的文件夹中创建文件这是由我创建的。我想从newFolder获取此文件并传递给iPhone中的邮件编辑器。如何在文档目录的子文件夹中创建文件?

这是创建文件夹在我的文档文件夹,但现在我想创建文件在这个文件夹名称newFolder帮助我在这。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"newFolder"]; 
    NSError *error = NULL; 
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
     [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; 

我写这篇文章的代码,但我的CSV文件中doument diretory文件夹中创建我想cretae我的文件夹,在这个文件是doument目录与名称newFolder

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *docDir = [paths objectAtIndex:0]; 

     NSString *filename = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"EmployeeBase.Csv"]]; 
      NSError *error = NULL; 
      BOOL written = [csvString writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:&error]; 
      if (!written) 
       NSLog(@"write failed, error=%@", error); 

回答

0

你可以写一个txt文件(可以说file.txt的)到指定目录(可以说newFolder)是这样的:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"newFolder"]; 
NSString *filePath = [dataPath stringByAppendingPathComponent:@"file.txt"]; 

NSString *str = @"THIS IS THE CONTENT OF THE TXT FILE"; 

[str writeToFile:filePath atomically:TRUE encoding:NSUTF8StringEncoding error:NULL]; 

我希望帮助S;)

12

这里是你自己的文件夹中的文件目录保存图像的例子...

UIImage *imageForShare = [UIImage imageNamed:@"anyImage.jpg"]; 
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"]; 
// New Folder is your folder name 
NSError *error = nil; 
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error]; 

NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"]; 
NSData *data = UIImageJPEGRepresentation(imageForShare, 1.0); 
[data writeToFile:fileName atomically:YES]; 

谢谢!!

+0

这是工作......但我仍然在寻找如何创建内部文件目录中的多个子文件夹中的一个,然后保存这些数据夹内的图片...任何帮助? –

+1

@PradeepReddyKypa只是不要追加扩展名,它会成为一个文件夹。你也可以使用'NSString * fileName = [stringPath stringByAppendingString:@“folderName/image.jpg”];'。 – TheTiger

2
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,  
    YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory 
    NSString *myPath = [documentsPath stringByAppendingPathComponent:@"/MyDocuments"]; 

    //Create folder 
    if (![[NSFileManager defaultManager] fileExistsAtPath:myPath]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:myPath withIntermediateDirectories:NO 
    attributes:nil error:NULL]; 

    NSString *newStr=[[NSString alloc] initWithFormat:@"Sample.txt"]; 
    NSString *filePath = [myPath stringByAppendingPathComponent:newStr]; //Add the file name 
    [pngData writeToFile:filePath atomically:YES]; //Write the file 
相关问题