zoukankan      html  css  js  c++  java
  • 每天一点小知识001--unity在ios系统的文件读取

    通过http://blog.csdn.net/joyhen/article/details/8572094这个博客修改的代码,unity在ios的文件操作必须通过流来读取写入。否则文件放在沙盒位置也是读不出来的

    using UnityEngine;
    using System.Collections;
    using System;
    using System.IO;
    using System.Text;
    
    public class FileHandle {
    
    private FileHandle(){}
    public static readonly FileHandle instance = new FileHandle();
    
    //the filepath if there is a file
    public bool isExistFile(string filepath){
    return File.Exists(filepath);
    }
    
    public bool IsExistDirectory(string directorypath){
    return Directory.Exists(directorypath);
    }
    
    public bool Contains(string Path,string seachpattern){
    try{
    string[] fileNames = GetFilenNames(Path,seachpattern,false);
    return fileNames.Length!=0;
    }
    catch{
    return false;
    }
    }
    
    //return a file all rows
    public static int GetLineCount(string filepath){
    string[] rows = File.ReadAllLines(filepath);
    return rows.Length;
    }
    
    public bool CreateFile(string filepath){
    try{
    if(!isExistFile(filepath)){
    StreamWriter sw;
    FileInfo file = new FileInfo(filepath);
    //FileStream fs = file.Create();
    //fs.Close();
    sw = file.CreateText();
    sw.Close();
    }
    }
    catch{
    return false;
    }
    return true;
    }
    
    public string[] GetFilenNames(string directorypath,string searchpattern,bool isSearchChild){
    if(!IsExistDirectory(directorypath)){
    throw new FileNotFoundException();
    }
    try{
    
    return Directory.GetFiles(directorypath,searchpattern,isSearchChild?SearchOption.AllDirectories:SearchOption.TopDirectoryOnly);
    }
    catch{
    return null;
    }
    
    }
    
    public void WriteText(string filepath,string content)
    {
    //File.WriteAllText(filepath,content);
    FileStream fs = new FileStream(filepath,FileMode.Append);
    StreamWriter sw = new StreamWriter(fs,Encoding.UTF8);
    sw.WriteLine(content);
    sw.Close();
    fs.Close();
    Debug.Log("write: filepath: "+filepath);
    Debug.Log("write: content: "+content);
    Debug.Log("写入完毕");
    }
    
    public void AppendText(string filepath,string content){
    File.AppendAllText(filepath,content);
    }
    
    public string FileToString(string filepath,Encoding encoding){
    FileStream fs = new FileStream(filepath,FileMode.Open,FileAccess.Read);
    StreamReader reader = new StreamReader(fs,encoding);
    try{
    return reader.ReadToEnd();
    }
    catch{
    return string.Empty;
    }
    finally{
    fs.Close();
    reader.Close();
    Debug.Log("读取完毕");
    }
    }
    
    public void ClearFile(string filepath){
    File.Delete(filepath);
    CreateFile(filepath);
    }
    
    }
  • 相关阅读:
    CF920E 补图强连通分量 BFS+set维护
    hdoj5876 补图最短路 BFS+set维护还可以更新的点.
    LA4287 tarjan求强连通分量+缩点
    cf920F SUM and REPLACE 树状数组+set 维护
    cf920G List Of Integers 二分+容斥
    cf 919D substring
    2017寒假概率dp训练题
    O(n)求数组中第k大的元素——堆排序
    O(n)求数组中第k大的元素——快排划分
    Turtlebot3入门手册之八:Realsense r200安装与测试
  • 原文地址:https://www.cnblogs.com/AXIA-zy/p/5101467.html
Copyright © 2011-2022 走看看