2011-04-11 67 views
68

如何在iOS上書寫文件?我試圖用下面的代碼做,但我做錯了:在iOS上書寫文件

char *saves = "abcd"; 
NSData *data = [[NSData alloc] initWithBytes:saves length:4]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile"]; 
[data writeToFile:appFile atomically:YES]; 

我在資源上創建了MyFile.txt。

+4

請準確描述出了什麼問題。 – 2011-04-11 10:17:21

回答

25

您的代碼在我結束工作,我都只是測試它。你在哪裏檢查你的改變?使用文檔目錄路徑。爲了獲得路徑 -

NSLog(@"%@",documentsDirectory); 

,並從控制檯複製路徑,然後打開Finder和按CMD + Shift + G鍵和粘貼路徑在這裏,然後打開你的文件

+0

我想這是我錯了..我已經創建MyFile內我的資源應用程序目錄.. Bu沒有改變那裏..我在哪裏檢查或創建文件? – 2011-04-11 10:26:20

+2

我已經更新了我的答案,看那裏 – saadnib 2011-04-11 10:56:21

107

可能這對你有用。

//Method writes a string to a text file 
-(void) writeToTextFile{ 
     //get the documents directory: 
     NSArray *paths = NSSearchPathForDirectoriesInDomains 
      (NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 

     //make a file name to write the data to using the documents directory: 
     NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 
                 documentsDirectory]; 
     //create content - four lines of text 
     NSString *content = @"One\nTwo\nThree\nFour\nFive"; 
     //save content to the documents directory 
     [content writeToFile:fileName 
         atomically:NO 
           encoding:NSUTF8StringEncoding 
             error:nil]; 

} 


//Method retrieves content from documents directory and 
//displays it in an alert 
-(void) displayContent{ 
     //get the documents directory: 
     NSArray *paths = NSSearchPathForDirectoriesInDomains 
         (NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 

     //make a file name to write the data to using the documents directory: 
     NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 
                 documentsDirectory]; 
     NSString *content = [[NSString alloc] initWithContentsOfFile:fileName 
                 usedEncoding:nil 
                  error:nil]; 
     //use simple alert from my library (see previous post for details) 
     [ASFunctions alert:content]; 
     [content release]; 

} 
+1

我無法找到我寫的文件,它似乎不存在 – 2011-04-11 10:18:19

+0

您可以更改文檔目錄路徑。 – 2011-04-11 10:29:31

+0

使用 - #define DOCUMENTS_FOLDER [NSHomeDirectory()stringByAppendingPathComponent:@「Documents」] – 2011-04-11 10:30:46

7

嘗試使

NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile"]; 

NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile.txt"]; 
9

斯威夫特

func saveFile() { 
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
    let documentsDirectory = paths[0] as! String 
    let fileName = "\(documentsDirectory)/textFile.txt" 
    let content = "Hello World" 
    content.writeToFile(fileName, atomically: false, encoding: NSUTF8StringEncoding, error: nil) 
} 

func loadFile() { 
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
    let documentsDirectory = paths[0] as! String 
    let fileName = "\(documentsDirectory)/textFile.txt" 
    let content: String = String(contentsOfFile: fileName, encoding: NSUTF8StringEncoding, error: nil)! 
    println(content) 
} 

斯威夫特2

func saveFile() { 
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
    let documentsDirectory = paths[0] 
    let fileName = "\(documentsDirectory)/textFile.txt" 
    let content = "Hello World" 
    do{ 
     try content.writeToFile(fileName, atomically: false, encoding: NSUTF8StringEncoding) 
    }catch _ { 

    } 

} 

func loadFile()->String { 
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
    let documentsDirectory = paths[0] 
    let fileName = "\(documentsDirectory)/textFile.txt" 
    let content: String 
    do{ 
     content = try String(contentsOfFile: fileName, encoding: NSUTF8StringEncoding) 
    }catch _{ 
     content="" 
    } 
    return content; 
} 

夫特3

func saveFile() { 
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 
    let documentsDirectory = paths[0] 
    let fileName = "\(documentsDirectory)/textFile.txt" 
    let content = "Hello World" 
    do{ 
     try content.write(toFile: fileName, atomically: false, encoding: String.Encoding.utf8) 
    }catch _ { 

    } 

} 

func loadFile()->String { 
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 
    let documentsDirectory = paths[0] 
    let fileName = "\(documentsDirectory)/textFile.txt" 
    let content: String 
    do{ 
     content = try String(contentsOfFile: fileName, encoding: String.Encoding.utf8) 
    } catch _{ 
     content="" 
    } 
    return content; 
}