In my project I have two file .txt (in Resources folder), how can I copy them inside documents folder?
The iphone does not have a docs folder. Any storage that this app offers is inside the app, likely on a server. You can go to the itunes page from which you downloaded the app and click the link to the Apps support site. Yes, the files will be on the app's folder.
With drag-and-drop on the iPhone, you can move seamlessly between apps to quickly upload photos, send links, share files and more. In a few seconds, you can quickly drag a dozen photos to send via text message or drop a link into an email.
Copies txtFile
from resource to document if not already present.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];
if ([fileManager fileExistsAtPath:txtPath] == NO) {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"txtFile" ofType:@"txt"];
[fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
}
If you want to overwrite every time then try this:
if ([fileManager fileExistsAtPath:txtPath] == YES) {
[fileManager removeItemAtPath:txtPath error:&error];
}
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"txtFile" ofType:@"txt"];
[fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
code for file is exist or not, if not then copy.
func CheckFileisExistOrNot(strPath:String) {
let filemgr = FileManager.default
if !filemgr.fileExists(atPath: strPath) {
let resorcePath = Bundle.main.path(forResource: "\(Global.g_databaseName)", ofType: ".db")
do {
try filemgr.copyItem(atPath: resorcePath!, toPath: strPath)
}catch{
print("Error for file write")
}
}
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0]stringByAppendingString:@"csvfile"];
NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"sample.csv"];
if ([fileManager fileExistsAtPath:txtPath] == NO) {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@".csv"];
[fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With