如何在文档目录的子文件夹中创建文件?

时间:2012-06-07 05:24:10

标签: iphone objective-c

我在文件目录中创建一个名为newFolder的文件夹。现在我想知道如何在我的文件夹中创建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文件夹中创建的我想在我的文件夹中创建这个文件,这是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);

3 个答案:

答案 0 :(得分: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];

Swift 4.0版本

guard let image = UIImage(named: "anyImage.jpg") else {return}
guard var stringPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first else {return}
stringPath = stringPath + "New Folder"

// New Folder is your folder name
if !FileManager.default.fileExists(atPath: stringPath) {
      do {
          try FileManager.default.createDirectory(atPath: stringPath, withIntermediateDirectories: false, attributes: nil)
      } catch let error {
          print(error.localizedDescription)
          return
      }
 }

 let fileName = stringPath + "/image.jpg"
 let url = URL(fileURLWithPath: fileName)
 if let data = UIImageJPEGRepresentation(image, 1.0) {
      do {
            try data.write(to: url)
        } catch let error {
            print(error.localizedDescription)
       }
 }

答案 1 :(得分: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

答案 2 :(得分: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];

我希望有所帮助;)