2012-06-07 26 views
5

Tworzę jeden folder w katalogu dokumentów o nazwie newFolder.So teraz chcę wiedzieć, jak utworzyć plik txt w moim folderze, który jest tworzony za pomocą newFolder i teraz jak utworzyć plik w katalogu dokumentów, ale jak utworzyć plik w moim folderze który jest tworzony przeze mnie. Chcę pobrać ten plik również z newFolder i przekazać do kompozytora poczty w telefonie iPhone.Jak utworzyć plik w podfolderze katalogu dokumentów?

To jest folder tworzenia w moim folderze dokumentów, ale teraz chcę utworzyć plik w tym folderze nazwa newFolder mi w tym pomóc.

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

napisać ten kod, ale mój plik CSV jest stworzenie w doument folderze diretory chcę cretae ten plik w moim folderze, który jest katalog doument mieć o nazwie 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); 

Odpowiedz

0

Można napisz plik txt (powiedzmy plik file.txt) do określonego katalogu (powiedzmy newFolder) w ten sposób:

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

Mam nadzieję, że pomoc S;)

12

Oto przykład zapisywania obrazu w katalogu dokumentów w swoim własnym folderze ...

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

Dziękuję !!

+0

To działa ... Ale wciąż szukam tego, w jaki sposób utworzyć wiele podkatalogów w katalogu dokumentów, a następnie zapisać obrazy w tych podfolderach. Czy jest jakaś pomoc? –

+1

@PradeepReddyKypa Po prostu nie dołączaj rozszerzenia i będzie to folder. Możesz także użyć '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 
Powiązane problemy