NSData *dataToWrite = [[NSString stringWithString:@"String to write"] dataUsingEncoding:NSUTF8StringEncoding];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"fileName.txt"];
// Write the file
[dataToWrite writeToFile:path atomically:YES];
// Read the file
NSString *stringFromFile = [[NSString alloc] initWithContentsOfFile:path];
// Check if file exists
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager fileExistsAtPath:path]; // Returns a BOOL
// Remove the file
[fileManager removeItemAtPath:path error:NULL];
// Cleanup
[stringFromFile release];
[fileManager release];
White log into the log-file
+ (void)logGenenalInfo:(NSString *)data {
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"GeneralFile.txt"];
// Check if file exists
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
// Read the file
NSString *stringFromFile = [[NSString alloc] initWithContentsOfFile:path];
data = [stringFromFile stringByAppendingFormat:@" %@",data];
}
NSData *dataToWrite = [data dataUsingEncoding:NSUTF8StringEncoding];
// Write the file
[dataToWrite writeToFile:path atomically:YES];
[fileManager release];
}
Clean the Log
+ (void)cleanAllLog {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *gePath = [docsDirectory stringByAppendingPathComponent:@"GeneralFile.txt"];
// Check if file exists then Remove the file
if ([fileManager fileExistsAtPath:gePath]) // Returns a BOOL
[fileManager removeItemAtPath:gePath error:NULL];
[fileManager release];
}
Get the program information
DateTime\tMessage\tData\tfileNameAndLineNumber
[LogFileHelper logGenenalInfo:
[NSString stringWithFormat:@"function %s, line %d \n"
,__FUNCTION__,__LINE__]];
No comments:
Post a Comment