7

我正在文档目录中创建一个名为 newFolder 的文件夹。所以现在我想知道如何在使用 newFolder 创建的文件夹中创建 txt 文件我现在如何在文档目录中创建文件但如何在我的文件夹中创建文件由我创建。我也想从 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 目录文件夹中创建的,我想在我的文件夹中创建这个文件,该文件夹名为 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);
4

3 回答 3

15

这是将图像保存在您自己文件夹中的文档目录中的示例...

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];

斯威夫特 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)
       }
 }
于 2012-06-07T05:57:55.620 回答
3
    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
于 2014-05-27T06:05:12.383 回答
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];

我希望这会有所帮助;)

于 2012-06-07T05:31:24.990 回答