将字符串写入文件时出现问题...(iPhone SDK) [英] Problem writing string to a file ...(iPhone SDK )

查看:470
本文介绍了将字符串写入文件时出现问题...(iPhone SDK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想抓取在webview中访问的所有URL,并将它们写入到文本文件中。我不想在 - (BOOL)webView方法中使用writeToFile coz,它将覆盖而不是附加到文件。我可以创建文件,但所有它写入该文件是字符串我用来创建文件使用FileManager createFileAtPath与内容...
也在 - (BOOL)的webView方法...当我试图看看是否文件是只读的或可写的(isWritableFileAtPath)它只读。
POSIX权限在viewDidLoad - > 511 ...
在终端去检查文件属性到该位置它的-rwxrwxrwx
我是新来堆栈溢出不知道如何在这里发布的代码使用pastebin ... http://pastebin.com/Tx7CsXVB

   - (void)viewDidLoad {
[super viewDidLoad];
[myBrowser loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@http://www.apple.com]]]; // UIWebView * myBrowser;是一个实例变量
NSFileManager * fileMgr = [NSFileManager defaultManager];
NSDictionary * fileAttrs = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:777] forKey:NSFilePosixPermissions]; / *为所有用户设置属性为rwx * /
NSDictionary * attribs; //写入文件之后读取属性
NSString * aPath = [NSHomeDirectory()stringByAppendingPathComponent:@Documents / file1.txt];
myBrowser.delegate = self; // UIWebView委托Self
//如果文件不存在创建一个
if([fileMgr fileExistsAtPath:aPath])
{
NSLog(@File exists on this位置);
}
else
{
NSString * someString = @这是文件的开头;
NSData * startString = [someString dataUsingEncoding:NSASCIIStringEncoding];
[fileMgr createFileAtPath:aPath contents:startString attributes:fileAttrs]; //更早的属性没有改为fileAttrs
}
NSLog(@aPath is%@,aPath);
attribs = [fileMgr attributesOfItemAtPath:aPath error:NULL];
NSLog(@Created on%@,[attribs objectForKey:NSFileCreationDate]);
NSLog(@File type%@,[attribs objectForKey:NSFileType]);
NSLog(@POSIX Permissions%@,[attribs objectForKey:NSFilePosixPermissions]);
}

// UIWebView委托在每次用户触及当前WebPage中的任何嵌入的URL时调用此方法。我想抓取所有访问的URL并将其写入文件。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)请求导航类型:(UIWebViewNavigationType)navigationType {
NSFileManager * fileManager = [NSFileManager defaultmanager];
NSString * path = [NSHomeDirectory()stringByAppendingPathComponent:@Documents / file1.txt];
fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
[fileHandle seekToEndOfFile]; //在fileHandleForWritingAtPath旁边移动,因为上面会把指针再次放到文件开头,所以设置句柄来寻找文件结尾
/ *代码段来检查那个路径上的文件是否可写* /
if([fileManager isWritableFileAtPath:path] == YES)
NSLog(@File is writable);
else
NSLog(@File is read only);
/ *代码段,检查该路径中的文件是否可写入ENDS * /
NSURL * url = request.URL;
NSString * currenturl = url.absoluteString;
NSString * currentURL = [NSString stringWithFormat:@%@ \\\
,currenturl];
NSString * str = [NSString stringWithFormat:@%@,currentURL]; / *已经被设置* /
[fileHandle writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
//测试字符串是否通过读取被写入文件...
NSData * dataBuffer = [fileMgr contentsAtPath:path];
NSString * some;
some = [[NSString alloc] initWithData:dataBuffer encoding:NSASCIIStringEncoding];
NSLog(@SOme String is:%@,some);
[fileHandle closeFile];


解决方案

我想你的问题可能是你正在测试Documents / file1.txt而不是/Documents/file1.txt

主要的'/'是重要的





我可以提出一个建议吗?把这个提炼出来,然后弄清楚是什么让它失败了?
我建议使用下面的表单,然后继续:

  if([fileManager isWritableFileAtPath:@/ Documents /file1.txt] == YES)
NSLog(@File is writable);
else
NSLog(@File is read only);

[/ edit]

I want to grab all URL's accessed in webview and write them to a text file.I don't want to use writeToFile coz in -(BOOL)webView method it would overwrite instead of appending to file. I can create file but all it writes to that file is string I used to create the file using FileManager createFileAtPath with Content ... Also in the -(BOOL) webView method... when I tried to see if the file is read-only or writable ( isWritableFileAtPath ) it gives read-only. POSIX Permissions in viewDidLoad -> 511 ... checked file attributes in terminal going to that location its -rwxrwxrwx I am new to Stack Overflow don't know how to post code here so using pastebin...http://pastebin.com/Tx7CsXVB

- (void)viewDidLoad {
    [super viewDidLoad];
    [myBrowser loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com"]]]; // UIWebView *myBrowser; is an instance variable
    NSFileManager *fileMgr=[NSFileManager defaultManager];
    NSDictionary* fileAttrs = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:777] forKey:NSFilePosixPermissions]; /*for setting attribute to rwx for all users */
    NSDictionary *attribs; // To read attributes after writing something to file
    NSString *aPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/file1.txt"]; 
    myBrowser.delegate = self; // UIWebView delegate Self
    // if the File Doesn't exist create one
    if([fileMgr fileExistsAtPath:aPath])
    {
        NSLog(@"File Exists at this Location");
    }
    else
    {
        NSString *someString = @"This is start of file";
        NSData *startString =[someString dataUsingEncoding: NSASCIIStringEncoding];
        [fileMgr createFileAtPath:aPath contents:startString attributes: fileAttrs]; // earlier attributes was nil changed to fileAttrs
    }
    NSLog(@"aPath is %@",aPath);
    attribs = [fileMgr attributesOfItemAtPath:aPath error: NULL];
    NSLog (@"Created on %@", [attribs objectForKey: NSFileCreationDate]);
    NSLog (@"File type %@", [attribs objectForKey: NSFileType]);
    NSLog (@"POSIX Permissions %@", [attribs objectForKey: NSFilePosixPermissions]);
}

//UIWebView delegate calls this method every time user touches any embedded URL's in the current WebPage. I want to grab all the URL's accessed and write them to file.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
        NSFileManager *fileManager =[NSFileManager defaultmanager];
    NSString *path =  [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/file1.txt"];
    fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
    [fileHandle seekToEndOfFile]; // Moved up next to fileHandleForWritingAtPath since the above would place pointer to start of file again so setting handle to seek to End of File
    /* section of code to check if the file at that path is writable or not */
    if ([fileManager isWritableFileAtPath:path]  == YES)
        NSLog (@"File is writable");
    else
        NSLog (@"File is read only");
    /* section of code to check if the file at that path is writable or not ENDS*/
    NSURL *url = request.URL;
    NSString *currenturl = url.absoluteString;
    NSString *currentURL = [NSString stringWithFormat:@"%@\n",currenturl];
    NSString *str =[NSString stringWithFormat:@"%@",currentURL];/* has already been set up */
    [fileHandle writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
    // testing if string has been written to file by reading it... 
    NSData *dataBuffer = [fileMgr contentsAtPath:path];
    NSString *some;
    some = [[NSString alloc] initWithData:dataBuffer encoding:NSASCIIStringEncoding];
    NSLog(@"SOme String is: %@",some);
    [fileHandle closeFile];
}

解决方案

I think your issue may be that you're testing Documents/file1.txt and not /Documents/file1.txt

The leading '/' is important

[edit]
May I make a suggestion? Distill this down to what works first and then figure out what makes it fail? I would recommend using the following form and continuing from there:

if ([fileManager isWritableFileAtPath: @"/Documents/file1.txt"] == YES)
   NSLog (@"File is writable");
else
   NSLog (@"File is read only");

[/edit]

这篇关于将字符串写入文件时出现问题...(iPhone SDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆