在 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。

139818 次浏览

也许这对你有用。

//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];


}

试试

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

作为

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

你的代码在我这边正常工作,我刚刚测试过。您在哪里检查您的更改?使用“文档”目录路径。为了得到路径

NSLog(@"%@",documentsDirectory);

从控制台复制路径,然后打开 finder,按 Cmd + shift + g,粘贴路径到这里,然后打开文件

斯威夫特

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)
}

Swift 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;
}

Swift 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;
}