2012-11-21 80 views
1

我嘗試通過編程創建HTML文件。但生成的文件顯示我爲空白文件。我不知道這個問題。創建HTML文件

這裏是我的代碼:

- (IBAction) createFileAction 
{ 
    NSLog(@"************* createFileAction *************"); 
    NSMutableString *htmlString = [[NSMutableString alloc] init]; 
    NSLog(@"%@",htmlString); 
    [htmlString appendString:@"<html><head><title></title><style type='text/css'>.style1{height: 27px;}#Text1{height: 19px;width: 210px;}.style2{width: 255px;}.style3{height: 27px;width: 255px;}#petValue{width: 206px;}#nameValue{width: 206px;}#dateValue{width: 209px;}"]; 
    [htmlString appendString:@"#Text2{width: 215px;}#Text3{width: 210px;height: 22px;}#Text4{width: 209px;}.style8{width: 13px;}.style9{ width: 263px;}.style10{height: 27px;width: 263px;}"]; 
    [htmlString appendString:@".style11{width: 418px;}.style12{width: 199px;}.style13{height: 27px; width: 199px;}.style14{height: 24px;}.style15{width: 410px;}</style> </head>"]; 
    [htmlString appendString:@"<body>"]; 

    NSString *originalString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.makepartsfast.com/2012/09/4337/more-3d-printing-in-metals-ex-one-introduces-the-m-flex-3d-printing-system/"] encoding:NSUTF8StringEncoding error:nil]; 
    NSScanner *scanner = [NSScanner scannerWithString:originalString]; 
    NSString *extractedString = nil; 


    [scanner scanUpToString:@"<div class=\"postarea\">" intoString:nil]; 
    [scanner scanString:@"<div class=\"postarea\">" intoString:nil]; 



    [scanner scanUpToString:@"<div style=\"clear:both;\">" intoString:&extractedString]; 


    if (extractedString) 
    { 
     // string was extracted 
     NSLog(@"%@", extractedString); 
    } 
    NSLog(@"BEFORE= %@",htmlString); 
    [htmlString appendString:extractedString]; 
    NSLog(@"AFTER= %@",htmlString); 
    [htmlString appendString:@"</body></html>"]; 
    NSLog(@"FINAL=%@",htmlString); 

    // check to see if file already exists in documents directory 
    if([self.fileMgr fileExistsAtPath:self.file]) 
    { 
     NSLog(@"File already exists"); 
    } 
    else 
    { 
     NSLog(@"file does not exist"); 
     NSLog(@"Try creating the file"); 

     // file does not already exist so create it 
     [self.fileMgr createFileAtPath:file contents:nil attributes:nil]; 

     NSLog(@"%@",htmlString); 
     [htmlString writeToFile:self.file atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil]; 


     //Test again whether this file exists now that we have tried creating it 
     if([self.fileMgr fileExistsAtPath:self.file]) 
     { 
      NSLog(@"File exists now"); 
     } 
     else 
     { 
      NSLog(@"File still doesn't exist"); 
     } 

    } 
} 

在這種方法中,我從URL中HTML格式& extractedString只選擇部分追加到htmlstring。當我嘗試追加不同的字符串,然後它工作正常。

回答

5

這是因爲你通過NSStringEncodingConversionAllowLossy作爲encoding的論點。你應該傳遞一個字符串編碼參數。發生了什麼是NSStringEncodingConversionAllowLossy等於1.但writeToFile:atomically:encoding:error:期望字符串編碼常量。字符串編碼常數等於1是NSASCIIStringEncoding。但是你的字符串不能轉換爲ASCII,所以你得到一個空文件。更改該行使用UTF8,你會得到預期的非空文件:

[htmlString writeToFile:self.file atomically:NO encoding:NSUTF8StringEncoding error:nil]; 

你有這個至少部分是因爲你不檢查返回類型或錯誤條件麻煩。如果出現任何問題,方法調用返回BOOL並將創建一個NSError。如果你在聽,這會幫助你找出錯誤的地方。如果你已經做到了這一點:

NSError *saveError = nil; 
if (![htmlString writeToFile:file atomically:NO encoding:NSStringEncodingConversionAllowLossy error:&saveError]) { 
    NSLog(@"Error writing file: %@", saveError); 
} 

你會看到一個錯誤信息讀取類似:

2012-11-21 11:20:47.250 Untitled[5731:707] Error writing file: Error Domain=NSCocoaErrorDomain Code=517 "The file 「junk.html」 couldn’t be saved using text encoding Western (ASCII)." UserInfo=0x7ffc0860b7c0 {NSFilePath=/tmp/junk.html, NSStringEncoding=1 

這可能不是第一次明顯但事實上,它特別提到嘗試使用ASCII會成爲解決問題根源的巨大線索。

+0

非常感謝!再次感謝! – user1673099