nsfilehandle filehandleforwritingatpath:nullを返します!
-
01-10-2019 - |
質問
私のiPadアプリには小さなダウンロード機能があり、NSFileHandleを使用してデータを追加したいと思います。問題は、作成コールのみがNULLファイルハンドルを返すことです。何が問題なのでしょうか?これが私のファイルハンドルを作成することになっているコードの3行です。
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName];
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
ファイルパスをチェックしましたが、何も悪いことは見えませんでした。
ティア
解決
fileHandleForWritingAtPath
「作成」呼びかけではありません。ドキュメントは明示的に次のように述べています。「戻り値:初期化されたファイルハンドル、または パスにファイルが存在しない場合はありません」(強調が追加されました)。ファイルが存在しない場合は、ファイルを作成したい場合は、次のようなものを使用する必要があります。
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
if(output == nil) {
[[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil];
output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
}
ファイルが既に存在する場合は、ファイルに追加する場合は、次のようなものを使用します [output seekToEndOfFile]
. 。その後、完全なコードは次のように見えます。
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName];
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
if(output == nil) {
[[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil];
output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
} else {
[output seekToEndOfFile];
}
他のヒント
ドキュメントディレクトリパスを取得します
+(NSURL *)getDocumentsDirectoryPath
{
return [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
}
ファイルの終了にテキストを保存します
ファイルが存在しない場合は、それを作成してデータを書き込みます
+(void)saveText:(NSString *)textTobeSaved atPath:(NSString*)fileName
{
NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];
NSString *path = [[self getDocumentsDirectoryPath].path
stringByAppendingPathComponent:filePath];
NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
if(fileHandler == nil) {
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
} else {
textTobeSaved = [NSString stringWithFormat:@"\n-----------------------\n %@",textTobeSaved];
[fileHandler seekToEndOfFile];
}
[fileHandler writeData:[textTobeSaved dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandler closeFile];
}
ファイルからテキストを取得します 指定されたファイル名付き
+(NSString *)getTextFromFilePath:(NSString *)fileName
{
NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];
NSString *path = [[self getDocumentsDirectoryPath].path
stringByAppendingPathComponent:filePath];
NSLog(@"%@",path);
if(path!=nil)
{
NSString *savedString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
return savedString;
}else{
return @"";
}
}
ファイルを削除する
+(void)deleteFile:(NSString *)fileName
{
NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];
NSString *path = [[self getDocumentsDirectoryPath].path
stringByAppendingPathComponent:filePath];
NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
if(fileHandler != nil) {
[[NSFileManager defaultManager]removeItemAtPath:path error:nil];
}
}
所属していません StackOverflow