Reputation: 1423
I am creating one folder in document directory with name newFolder.So now i want to know how to create txt file in my folder which is create with newFolder i now how to create file in document directory but how to create file in my folder which is create by me.I want to fetch this file also from newFolder and pass to mail composer in iPhone.
This is create folder in my document folder but now i want create file in this folder name newFolder help me in this.
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];
i write this code but my csv file is create in doument diretory folder i want to cretae this file in my folder which is doument directory have with name 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);
Upvotes: 7
Views: 10254
Reputation: 13364
Here is an example of saving image in documents directory in your own folder...
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 Version
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)
}
}
Upvotes: 15
Reputation: 1663
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
Upvotes: 3
Reputation: 2393
You can write a txt file (lets say file.txt) to a specific directory (lets say newFolder) like this:
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];
I hope that helps ;)
Upvotes: 0