unity+本地创建.txt(.ini)

创建本地.txt(.ini)

public Text readText;
    private string path;
    private string path2;
    private string content;
    private string[] contents;
    // Use this for initialization
    void Start () {
        path = Application.dataPath + "\\test.txt";//创建.ini文件直接将后缀由.txt改为.ini即可
        path2 = Application.dataPath + "\\test1.txt";
        content = "窗前明月光|疑是地上霜|举头望明月|低头思故乡";//内容改为自己需要的即可,比如网址之类的
        contents = content.Split('|');
    }
    
    // Update is called once per frame
    void Update () {
        
    }
    //写入数据
    public void WriteTxt()
    {
        if(!File.Exists(path))
        {
            //文本不存在创建文本
            FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fileStream, Encoding.UTF8);
            foreach (var item in contents)
            {
                sw.WriteLine(item);//WriteLine每次最后结尾也会带一个回车符号,如果只有一行内容建议使用Write,比如项目中只需要写入网址,那么就只需要一行就行
            }
            sw.Close();
            fileStream.Close();
          
        }
        else
        {
           
        }
    }
    //读取数据
    public void ReadTxt()
    {
        if(File.Exists(path))
        {
            StreamReader sr = new StreamReader(path);
            readText.text = sr.ReadToEnd();
            sr.Close();
        }     
    }
    //删除文本
    public void DeleTxt()
    {
        FileInfo fileInfo = new FileInfo(path);
        fileInfo.Delete();
          
    }
    //拷贝文本
    public void CopyTxt()
    {
        if(File.Exists(path)&&File.Exists(path2))
        {
            string content;
            StreamReader sr = new StreamReader(path);
            content= sr.ReadToEnd();
            StreamWriter sw = new StreamWriter(path2);
            sw.Write(content);              
            sr.Close();
            sw.Close();          
        }
    }
}

 

上一篇:GRPC-go版本


下一篇:docker svn仓库搭建及hook配置