创建plist并写入

1. 创建一个plist文件

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *path=[paths    objectAtIndex:0];

NSLog(@"path = %@",path);

NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];    //文件的完整路径

NSFileManager* fm = [NSFileManager defaultManager];

[fm createFileAtPath:filename contents:nil attributes:nil];      //根据完整路径创建文件

2. 创建一个dic,写到plist文件里

NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:@"sina",@"1",@"163",@"2",nil];

[dic writeToFile:filename atomically:YES];

3. 读文件内容

NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];

NSLog(@"dic is:%@",dic2);

时间: 2024-08-08 22:08:57

创建plist并写入的相关文章

iOS中plist的创建,数据写入与读取

iOS中plist的创建,数据写入与读取功能创建一个test.plist文件,textInput作为输入,displayLabel作为显示,有一个按钮来触发保持程序triggerStorage: -(void)triggerStorage { displayLabel.text = textInput.text; NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)

蜗牛爱课- iOS中plist的创建,数据写入与读取

iOS中plist的创建,数据写入与读取功能创建一个test.plist文件-(void)triggerStorage{    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);    NSString *path=[paths    objectAtIndex:0];      NSString *filename=[path stringByAppendin

小程序1:输入一个文件名,检测是否存在;如果不存在创建文件并写入内容

实现内容:输入一个文件名,检测是否存在:如果不存在创建文件并写入内容代码实现如下: 注意事项: 1.python os.path模块中  os.path.exists(path)  #路径存在则返回True,路径损坏返回False:::所以输入应该是“路径”,不是随意一个文件夹 2.IOError: [Errno 22] invalid mode ('r') or filename 这种错误的出现是在使用built-in函数file()或者open()的时候:因为文件的打开模式不对或文件名有问题

PHP 文件操作类(创建文件并写入) 生成日志

<?php /** * 文件操作(生成日志)支持多条插入 * (如果插入多条语句并换行 用','逗号分开) * */ class log { public $path = './info.txt'; //默认值文件 public $mode = 'a'; //默认追加写 public $content = '默认值:空'; //默认内容是 空 public function addlog($path = null, $mode = null, $content = null) { //判断写入的

数据持久化-Plist文件写入

数据持久化,常见4种:归档,plist文件,sqlite,coreData.今天复习的是plist文件读写. // // ViewController.m // Test_Plist // // Created by lidongbo on 14/10/30. // Copyright (c) 2014年 lidongbo. All rights reserved. // #import "ViewController.h" @interface ViewController () @

创建plist文件

可以先在工程中直接新建一个plist文件,往里面写入自己需要的数据.但是这里的plist文件我们无法修改,是只读的,我们可以将这个plist文件复制一份到沙盒中,然后对沙盒中的文件进行操作.具体代码如下: 从自己建立的plist文件 复制到沙盒中 : //先获得沙盒路径 NSArray *storeFilePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);        N

[Xcode10 实际操作]七、文件与数据-(9)编码创建Plist文件(属性列表文件)

本文将演示如何通过编码的方式,创建属性列表文件. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] 1 import UIKit 2 3 class ViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view, typically from

php学习笔记--高级教程--读取文件、创建文件、写入文件

打开文件:fopen:fopen(filename,mode);//fopen("test.txt","r"): 打开模式:r  仅仅读方式打开,将文件指针指向文件头 r+  读写方式打开,将文件指针指向文件头 w  写入方式,指向文件头,假设不存在则尝试创建 w+ 读写方式,指向文件头,假设不存在则尝试创建 a  写入方式打开,指向文件末尾,假设不存在则尝试创建 a+ 读写方式打开,指向文件末尾,假设不存在则尝试创建 读取文件:fread:fread(); rea

java io流 创建文件、写入数据、设置输出位置

java io流 创建文件 写入数据 改变system.out.print的输出位置 //创建文件 //写入数据 //改变system.out.print的输出位置 import java.io.*; public class Index{ public static void main(String[] args) throws Exception{ /** * 存储为二进制,给计算机看的 */ //创建文件 DataOutputStream sjl = new DataOutputStrea